# equation solution in integer

72 views

### Bert Henry

Apr 17, 2020, 1:17:12 PM4/17/20
to sage-support
I have the equation
x + y = 15
an 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?

Bert Henry

### Bert Henry

Apr 17, 2020, 2:16:30 PM4/17/20
to sage-support
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`

### Matthias Koeppe

Apr 17, 2020, 3:43:25 PM4/17/20
to sage-support

### Bert Henry

Apr 18, 2020, 1:35:17 AM4/18/20
to sage-support
@Matthias,
thanks for your answer, but I don‘t underrstand it. In your link, I can‘t fInd the solution for my problem. Would you give me a hunt, where to search?

Am Freitag, 17. April 2020 19:17:12 UTC+2 schrieb Bert Henry:

### slelievre

Apr 18, 2020, 4:11:05 PM4/18/20
to sage-support
Matthias is hinting at a possible reformulation
of the problem as finding integral points in a
polyhedron. Let me expand.

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:

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))

### Bert Henry

Apr 18, 2020, 7:41:15 PM4/18/20
to sage-support

wow, I didn‘t expect, that may „simple“ problem needs such deep math. I will look for the math of polyhedrons to understand, what you wrote, because in some number-crosswords (I don‘t know the correct english word) you search for solutions of the m entioned type. Also you need it in some amphanumerics like SEND+MORE=MONEY.

Bert

Am Freitag, 17. April 2020 19:17:12 UTC+2 schrieb Bert Henry:

### Dima Pasechnik

Apr 18, 2020, 7:51:43 PM4/18/20
to sage-support
On Sun, Apr 19, 2020 at 7:41 AM Bert Henry <bert...@gmail.com> wrote:
>
>
> wow, I didn‘t expect, that may „simple“ problem needs such deep math. I will look for the math of polyhedrons to understand, what you wrote, because in some number-crosswords (I don‘t know the correct english word) you search for solutions of the m entioned type. Also you need it in some amphanumerics like SEND+MORE=MONEY.
>

my maths teacher pointed to us that when one compares numbers by
counting digits, one is actually doing logarithms in base 10 :-)

> Thanks a lot for answering
> Bert
>
>
> Am Freitag, 17. April 2020 19:17:12 UTC+2 schrieb Bert Henry:
>>
>> I have the equation
>> x + y = 15
>> an 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?
>>
>> Bert Henry
>
> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.

### Pedro A. Garcia

Apr 19, 2020, 4:29:02 AM4/19/20
to sage-support
May be you want to use ` WeightedIntegerVectors(15,[1,1])` or restricted partitions inside gap. This, for a single linear Diophantine equation like yours might be a fast approach.

Pedro

### Vincent Delecroix

Apr 19, 2020, 4:40:22 AM4/19/20
In this particular instance, I would rather write down the loop.
It is not hard to make the loop general for any bound on x and
y and any sum.

sum = 15
xmin = 1
xmax = 9
ymin = 1
ymax = 9
for x in range(max(xmin, sum-ymax), min(xmax, sum-ymin)+1):
print(x, sum-x)

which does run through

6 9
7 8
8 7
9 6