ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 19 Apr 2020 16:18:49 +0200Equation problemhttps://ask.sagemath.org/question/50839/equation-problem/I have the equation
x + y = 15
and I'm looking for solution only in the range x=1..9 and y=1..9, x and y both integer
Is there a sage-command to do that?
I tried it with
var('x, y')
assume(x,"integer")
assume(x>0)
assume(y, "integer")
assume(y>0)
solve(x+y==15,x,y)
The result was
(t_0, -t_0 + 15)
obviously right, but not 6,9 7,8 8,7 and 9,6
Thanks in advance
Bert HenrySat, 18 Apr 2020 16:01:45 +0200https://ask.sagemath.org/question/50839/equation-problem/Comment by slelievre for <p>I have the equation</p>
<pre><code>x + y = 15
</code></pre>
<p>and I'm looking for solution only in the range x=1..9 and y=1..9, x and y both integer
Is there a sage-command to do that?</p>
<p>I tried it with</p>
<pre><code>var('x, y')
assume(x,"integer")
assume(x>0)
assume(y, "integer")
assume(y>0)
solve(x+y==15,x,y)
</code></pre>
<p>The result was</p>
<pre><code>(t_0, -t_0 + 15)
</code></pre>
<p>obviously right, but not 6,9 7,8 8,7 and 9,6</p>
<p>Thanks in advance
Bert Henry</p>
https://ask.sagemath.org/question/50839/equation-problem/?comment=50848#post-id-50848Note: also asked at:
- [sage-support, 2020-04-18, equation solution in integer](https://groups.google.com/d/topic/sage-support/iZxhH28DmaI/discussion)Sat, 18 Apr 2020 22:09:32 +0200https://ask.sagemath.org/question/50839/equation-problem/?comment=50848#post-id-50848Comment by Bert Henry for <p>I have the equation</p>
<pre><code>x + y = 15
</code></pre>
<p>and I'm looking for solution only in the range x=1..9 and y=1..9, x and y both integer
Is there a sage-command to do that?</p>
<p>I tried it with</p>
<pre><code>var('x, y')
assume(x,"integer")
assume(x>0)
assume(y, "integer")
assume(y>0)
solve(x+y==15,x,y)
</code></pre>
<p>The result was</p>
<pre><code>(t_0, -t_0 + 15)
</code></pre>
<p>obviously right, but not 6,9 7,8 8,7 and 9,6</p>
<p>Thanks in advance
Bert Henry</p>
https://ask.sagemath.org/question/50839/equation-problem/?comment=50858#post-id-50858Dear sielievre, I posted it first in the Google-Group, thats true, but afterwards I recognised, that that group was for more technical questions.Sun, 19 Apr 2020 11:28:57 +0200https://ask.sagemath.org/question/50839/equation-problem/?comment=50858#post-id-50858Comment by slelievre for <p>I have the equation</p>
<pre><code>x + y = 15
</code></pre>
<p>and I'm looking for solution only in the range x=1..9 and y=1..9, x and y both integer
Is there a sage-command to do that?</p>
<p>I tried it with</p>
<pre><code>var('x, y')
assume(x,"integer")
assume(x>0)
assume(y, "integer")
assume(y>0)
solve(x+y==15,x,y)
</code></pre>
<p>The result was</p>
<pre><code>(t_0, -t_0 + 15)
</code></pre>
<p>obviously right, but not 6,9 7,8 8,7 and 9,6</p>
<p>Thanks in advance
Bert Henry</p>
https://ask.sagemath.org/question/50839/equation-problem/?comment=50864#post-id-50864It's fine to ask at either place. Some prefer the mailing list format, others the Q&A website.
When a question is asked in multiple places it's nice if they link to each other so when someone later has a similar question they can see the answers at all places.Sun, 19 Apr 2020 16:18:49 +0200https://ask.sagemath.org/question/50839/equation-problem/?comment=50864#post-id-50864Comment by Juanjo for <p>I have the equation</p>
<pre><code>x + y = 15
</code></pre>
<p>and I'm looking for solution only in the range x=1..9 and y=1..9, x and y both integer
Is there a sage-command to do that?</p>
<p>I tried it with</p>
<pre><code>var('x, y')
assume(x,"integer")
assume(x>0)
assume(y, "integer")
assume(y>0)
solve(x+y==15,x,y)
</code></pre>
<p>The result was</p>
<pre><code>(t_0, -t_0 + 15)
</code></pre>
<p>obviously right, but not 6,9 7,8 8,7 and 9,6</p>
<p>Thanks in advance
Bert Henry</p>
https://ask.sagemath.org/question/50839/equation-problem/?comment=50846#post-id-50846Why not this?
[(x,15-x) for x in [1..9] if 15-x in [1..9] ]Sat, 18 Apr 2020 19:04:23 +0200https://ask.sagemath.org/question/50839/equation-problem/?comment=50846#post-id-50846Comment by Bert Henry for <p>I have the equation</p>
<pre><code>x + y = 15
</code></pre>
<p>and I'm looking for solution only in the range x=1..9 and y=1..9, x and y both integer
Is there a sage-command to do that?</p>
<p>I tried it with</p>
<pre><code>var('x, y')
assume(x,"integer")
assume(x>0)
assume(y, "integer")
assume(y>0)
solve(x+y==15,x,y)
</code></pre>
<p>The result was</p>
<pre><code>(t_0, -t_0 + 15)
</code></pre>
<p>obviously right, but not 6,9 7,8 8,7 and 9,6</p>
<p>Thanks in advance
Bert Henry</p>
https://ask.sagemath.org/question/50839/equation-problem/?comment=50857#post-id-50857Thanks a lot for your answers!!! I will try them all. Perhaps with some additional conditions: x != y, z as a third variable and perhaps two equations.
Sun, 19 Apr 2020 11:27:05 +0200https://ask.sagemath.org/question/50839/equation-problem/?comment=50857#post-id-50857Answer by John Palmieri for <p>I have the equation</p>
<pre><code>x + y = 15
</code></pre>
<p>and I'm looking for solution only in the range x=1..9 and y=1..9, x and y both integer
Is there a sage-command to do that?</p>
<p>I tried it with</p>
<pre><code>var('x, y')
assume(x,"integer")
assume(x>0)
assume(y, "integer")
assume(y>0)
solve(x+y==15,x,y)
</code></pre>
<p>The result was</p>
<pre><code>(t_0, -t_0 + 15)
</code></pre>
<p>obviously right, but not 6,9 7,8 8,7 and 9,6</p>
<p>Thanks in advance
Bert Henry</p>
https://ask.sagemath.org/question/50839/equation-problem/?answer=50847#post-id-50847This is a pure Python solution which also works in Sage:
sage: [(x,y) for x in range(1, 10) for y in range(1, 10) if x+y==15]
[(6, 9), (7, 8), (8, 7), (9, 6)]Sat, 18 Apr 2020 19:42:46 +0200https://ask.sagemath.org/question/50839/equation-problem/?answer=50847#post-id-50847Answer by FrédéricC for <p>I have the equation</p>
<pre><code>x + y = 15
</code></pre>
<p>and I'm looking for solution only in the range x=1..9 and y=1..9, x and y both integer
Is there a sage-command to do that?</p>
<p>I tried it with</p>
<pre><code>var('x, y')
assume(x,"integer")
assume(x>0)
assume(y, "integer")
assume(y>0)
solve(x+y==15,x,y)
</code></pre>
<p>The result was</p>
<pre><code>(t_0, -t_0 + 15)
</code></pre>
<p>obviously right, but not 6,9 7,8 8,7 and 9,6</p>
<p>Thanks in advance
Bert Henry</p>
https://ask.sagemath.org/question/50839/equation-problem/?answer=50844#post-id-50844Using polyhedrons (here a simplified example) :
sage: P = Polyhedron(ieqs=[[0,1,0],[0,0,1]],eqns=[[-15,1,1]]) ; P
A 1-dimensional polyhedron in QQ^2 defined as the convex hull of 2 vertices
sage: P.integral_points()
((0, 15),
(1, 14),
(2, 13),
(3, 12),
(4, 11),
(5, 10),
(6, 9),
(7, 8),
(8, 7),
(9, 6),
(10, 5),
(11, 4),
(12, 3),
(13, 2),
(14, 1),
(15, 0))
Sat, 18 Apr 2020 18:58:04 +0200https://ask.sagemath.org/question/50839/equation-problem/?answer=50844#post-id-50844Answer by slelievre for <p>I have the equation</p>
<pre><code>x + y = 15
</code></pre>
<p>and I'm looking for solution only in the range x=1..9 and y=1..9, x and y both integer
Is there a sage-command to do that?</p>
<p>I tried it with</p>
<pre><code>var('x, y')
assume(x,"integer")
assume(x>0)
assume(y, "integer")
assume(y>0)
solve(x+y==15,x,y)
</code></pre>
<p>The result was</p>
<pre><code>(t_0, -t_0 + 15)
</code></pre>
<p>obviously right, but not 6,9 7,8 8,7 and 9,6</p>
<p>Thanks in advance
Bert Henry</p>
https://ask.sagemath.org/question/50839/equation-problem/?answer=50849#post-id-50849Expanding on hints by Matthias Köppe on sage-support
and by FrédéricC here.
In RR^2, consider the set S of all (x, y) satisfying:
x >= 1
x <= 9
y >= 1
y <= 9
x + y = 15
or if one prefers,
-1 + x >= 0
9 - x >= 0
-1 + y >= 0
9 - y >= 0
-15 + x + y = 0
Since all the conditions used to define this set
are of one of the following forms:
(linear form in x and y) = 0
(linear form in x and y) >= 0
the subset S is what is called a "polyhedron" in R^2.
The problem in your original post can now be
rephrased as:
Find all integral points in the polyhedron S.
An introduction to polyhedra in Sage is at:
http://doc.sagemath.org/html/en/reference/discrete_geometry/sage/geometry/polyhedron/constructor.html
The polyhedron S can be input as
S = Polyhedron(ieqs=[[-1, 1, 0], [9, -1, 0], [-1, 0, 1], [9, 0, -1]], eqns=[[-15, 1, 1]])
Check that our input represents the correct polyhedron:
sage: print(S.Hrepresentation_str())
x0 + x1 == 15
-x0 >= -9
x0 >= 6
Find all integral points:
sage: S.integral_points()
((6, 9), (7, 8), (8, 7), (9, 6))
Sat, 18 Apr 2020 22:11:32 +0200https://ask.sagemath.org/question/50839/equation-problem/?answer=50849#post-id-50849Answer by Cyrille for <p>I have the equation</p>
<pre><code>x + y = 15
</code></pre>
<p>and I'm looking for solution only in the range x=1..9 and y=1..9, x and y both integer
Is there a sage-command to do that?</p>
<p>I tried it with</p>
<pre><code>var('x, y')
assume(x,"integer")
assume(x>0)
assume(y, "integer")
assume(y>0)
solve(x+y==15,x,y)
</code></pre>
<p>The result was</p>
<pre><code>(t_0, -t_0 + 15)
</code></pre>
<p>obviously right, but not 6,9 7,8 8,7 and 9,6</p>
<p>Thanks in advance
Bert Henry</p>
https://ask.sagemath.org/question/50839/equation-problem/?answer=50854#post-id-50854In fact what you ask is the use of Fourier-Motzkin elimination. It can be obtain directly because Maxima implement it. But I have not been able to obtain all the solution in one program :
for [6,9] & [9, 6] you may use
var('x, y')
assume(x,"integer")
assume(y, "integer")
solve([x + y <= 15, x + y >= 15, x >=1, y>=1, x<=9, y<=9], x, y)
for [7,8] & [8, 7]you may use
var('x, y')
assume(x,"integer")
assume(y, "integer")
solve([x + y <= 15, x + y >= 15, x >=7, y>=7, x<=9, y<=9], x, y)
In fact I was persuaded that Maxima gives all the solution in one shotSun, 19 Apr 2020 09:52:53 +0200https://ask.sagemath.org/question/50839/equation-problem/?answer=50854#post-id-50854Answer by vdelecroix for <p>I have the equation</p>
<pre><code>x + y = 15
</code></pre>
<p>and I'm looking for solution only in the range x=1..9 and y=1..9, x and y both integer
Is there a sage-command to do that?</p>
<p>I tried it with</p>
<pre><code>var('x, y')
assume(x,"integer")
assume(x>0)
assume(y, "integer")
assume(y>0)
solve(x+y==15,x,y)
</code></pre>
<p>The result was</p>
<pre><code>(t_0, -t_0 + 15)
</code></pre>
<p>obviously right, but not 6,9 7,8 8,7 and 9,6</p>
<p>Thanks in advance
Bert Henry</p>
https://ask.sagemath.org/question/50839/equation-problem/?answer=50855#post-id-50855More straightforward
sage: sum = 15
sage: xmin = 1
sage: xmax = 9
sage: ymin = 1
sage: ymax = 9
sage: for x in range(max(xmin, sum-ymax), min(xmax, sum-ymin)+1):
....: print(x, sum-x)
6 9
7 8
8 7
9 6
Sun, 19 Apr 2020 10:34:55 +0200https://ask.sagemath.org/question/50839/equation-problem/?answer=50855#post-id-50855