equation solution in integer

70 views
Skip to first unread message

Bert Henry

unread,
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?

Thanks in advance
Bert Henry

Bert Henry

unread,
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

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

Bert Henry

unread,
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

unread,
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

unread,
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.

Thanks a lot for answering
Bert


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

Dima Pasechnik

unread,
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?
>>
>> Thanks in advance
>> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/9f7a36c6-4f04-4767-b116-d5eca7d9ab36%40googlegroups.com.

Pedro A. Garcia

unread,
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

unread,
Apr 19, 2020, 4:40:22 AM4/19/20
to sage-s...@googlegroups.com
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

Pedro A. Garcia

unread,
Apr 19, 2020, 4:50:44 AM4/19/20
to sage-support
Yes, @vdelecroix, in these cases this might be a very good option. 

sage: list( [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]]

For a more general setting, normaliz, 4ti2 and other approaches are better; some are available in sage as pointed above.
Reply all
Reply to author
Forward
0 new messages