Linear Diophantine Equations
A Diophantine equation asks for integer solutions only. The simplest kind, , already shows up in practice: can a vending machine pay out exactly 1 euro with 7-cent and 11-cent coins? Which time steps does a sensor sampled every 12 ms share with one sampled every 18 ms? The extended Euclidean algorithm answers all of these.
When is there a solution?
Let . The equation has integer solutions if and only if .
- If a solution exists, divides and , so it divides .
- Conversely, if , take Bézout's (The Extended Euclidean Algorithm) and multiply by : , .
So has no integer solutions (3 does not divide 7), while does.
All the solutions
If is one solution, every solution is
Substituting shows these work: the two added terms cancel. And there are no others, because two solutions differ by with ; dividing by and using that and are coprime forces . The solutions lie on a line, evenly spaced.
def ext_gcd(a, b):
r0, x0, y0, r1, x1, y1 = a, 1, 0, b, 0, 1
while r1:
q = r0 // r1
r0, r1, x0, x1, y0, y1 = r1, r0 - q * r1, x1, x0 - q * x1, y1, y0 - q * y1
return r0, x0, y0
def solve(a, b, c):
g, x0, y0 = ext_gcd(a, b)
if c % g:
return None
k = c // g
return x0 * k, y0 * k, b // g, a // g # x = x0 + (b/g) t, y = y0 - (a/g) t
print(solve(6, 9, 7))
x0, y0, dx, dy = solve(6, 9, 12)
print("particular:", x0, y0, " step:", dx, -dy)
print([(x0 + dx * t, y0 - dy * t) for t in range(-2, 3)])None
particular: -4 4 step: 3 -2
[(-10, 8), (-7, 6), (-4, 4), (-1, 2), (2, 0)]
Non-negative solutions: the coin problem
With 7-cent and 11-cent coins, which amounts can be paid exactly? We need with . Using the general solution, choose so that is the smallest non-negative value and check whether is still non-negative.
def pay(c, a=7, b=11):
for y in range(c // b + 1): # small search is fine here
if (c - b * y) % a == 0:
return (c - b * y) // a, y
return None
print("100 cents:", pay(100))
impossible = [c for c in range(1, 80) if pay(c) is None]
print("impossible amounts:", impossible)
print("largest:", max(impossible), " formula ab - a - b =", 7 * 11 - 7 - 11)100 cents: (8, 4)
impossible amounts: [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 13, 15, 16, 17, 19, 20, 23, 24, 26, 27, 30, 31, 34, 37, 38, 41, 45, 48, 52, 59]
largest: 59 formula ab - a - b = 59
For coprime and , the largest amount that cannot be paid is (the Frobenius number, a theorem of Sylvester); every larger amount can.
In software
- Scheduling and animation: two loops with periods 12 and 18 line up every ticks, and the offsets at which they can coincide are governed by .
- Integer linear programming solvers and compilers (loop dependence analysis, the "GCD test") check whether array accesses
a[6i + 2]anda[9j + 5]can ever touch the same element by asking whether is solvable.
Exercises
Exercise 1: Solvable?
Which of these have integer solutions: (a) ; (b) ; (c) ?
Answer
(a) yes, (b) no, (c) yes.
Solution
divides 35 but not 30. divides everything.
Exercise 2: General solution by hand
Find all integer solutions of .
Answer
, .
Solution
, , so . One solution is , , and with the steps are 5 and 17.
Exercise 3: Tickets (code)
Adult tickets cost 13 and child tickets 8, and a group paid 211 in total. List every possible (adults, children) pair.
Answer
, .
Solution
Solve with ; solutions are spaced by 8 in .
print([(x, (211 - 13 * x) // 8) for x in range(0, 211 // 13 + 1) if (211 - 13 * x) % 8 == 0])[(7, 15), (15, 2)]
Check: and .
Test yourself
The equation has integer solutions exactly when…
- and are prime
If solves with , the next solution is…
With coins of 3 and 5, the largest amount that cannot be paid is…
- 7
- 8
- 15
- 1