TExcelTAD
Home › Number Theory & Cryptography › Linear Diophantine Equations

Linear Diophantine Equations

A Diophantine equation asks for integer solutions only. The simplest kind, ax+by=cax + by = c, 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 g=gcd⁡(a,b)g = \gcd(a, b). The equation ax+by=cax + by = c has integer solutions if and only if g∣cg \mid c.

  • If a solution exists, gg divides aa and bb, so it divides ax+by=cax + by = c.
  • Conversely, if c=kgc = kg, take Bézout's ax0+by0=gax_0 + by_0 = g (The Extended Euclidean Algorithm) and multiply by kk: x=kx0x = kx_0, y=ky0y = ky_0.

So 6x+9y=76x + 9y = 7 has no integer solutions (3 does not divide 7), while 6x+9y=126x + 9y = 12 does.

All the solutions

If (x0,y0)(x_0, y_0) is one solution, every solution is

x=x0+bgt,y=y0−agt,t∈Zx = x_0 + \frac{b}{g}t, \qquad y = y_0 - \frac{a}{g}t, \qquad t \in \mathbb{Z}

Substituting shows these work: the two added terms abgt−abgt\frac{ab}{g}t - \frac{ab}{g}t cancel. And there are no others, because two solutions differ by (Δx,Δy)(\Delta x, \Delta y) with aΔx=−bΔya\Delta x = -b\Delta y; dividing by gg and using that a/ga/g and b/gb/g are coprime forces bg∣Δx\frac{b}{g} \mid \Delta x. The solutions lie on a line, evenly spaced.

python · try it yourself
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 7x+11y=c7x + 11y = c with x,y≥0x, y \ge 0. Using the general solution, choose tt so that xx is the smallest non-negative value and check whether yy is still non-negative.

python · try it yourself
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 aa and bb, the largest amount that cannot be paid is ab−a−bab - a - b (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 lcm⁡(12,18)=36\operatorname{lcm}(12, 18) = 36 ticks, and the offsets at which they can coincide are governed by 12x−18y=c12x - 18y = c.
  • Integer linear programming solvers and compilers (loop dependence analysis, the "GCD test") check whether array accesses a[6i + 2] and a[9j + 5] can ever touch the same element by asking whether 6i−9j=36i - 9j = 3 is solvable.

Exercises

Exercise 1: Solvable?

Which of these have integer solutions: (a) 14x+21y=3514x + 21y = 35; (b) 14x+21y=3014x + 21y = 30; (c) 17x+5y=117x + 5y = 1?

Answer

(a) yes, (b) no, (c) yes.

Solution

gcd⁡(14,21)=7\gcd(14, 21) = 7 divides 35 but not 30. gcd⁡(17,5)=1\gcd(17, 5) = 1 divides everything.

Exercise 2: General solution by hand

Find all integer solutions of 17x+5y=117x + 5y = 1.

Answer

x=−2+5tx = -2 + 5t, y=7−17ty = 7 - 17t.

Solution

17=3⋅5+217 = 3 \cdot 5 + 2, 5=2⋅2+15 = 2 \cdot 2 + 1, so 1=5−2⋅2=5−2(17−3⋅5)=7⋅5−2⋅171 = 5 - 2 \cdot 2 = 5 - 2(17 - 3 \cdot 5) = 7 \cdot 5 - 2 \cdot 17. One solution is x=−2x = -2, y=7y = 7, and with g=1g = 1 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

(15,2)(15, 2), (7,15)(7, 15).

Solution

Solve 13x+8y=21113x + 8y = 211 with x,y≥0x, y \ge 0; solutions are spaced by 8 in xx.

python · try it yourself
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: 13⋅15+8⋅2=195+16=21113 \cdot 15 + 8 \cdot 2 = 195 + 16 = 211 and 13⋅7+8⋅15=91+120=21113 \cdot 7 + 8 \cdot 15 = 91 + 120 = 211.

Test yourself

The equation ax+by=cax + by = c has integer solutions exactly when…

  1. c>0c > 0
  2. gcd⁡(a,b)∣c\gcd(a, b) \mid c
  3. aa and bb are prime
  4. a+b=ca + b = c

If (x0,y0)(x_0, y_0) solves ax+by=cax + by = c with g=gcd⁡(a,b)g = \gcd(a, b), the next solution is…

  1. (x0+1,y0−1)(x_0 + 1, y_0 - 1)
  2. (x0+b/g, y0−a/g)(x_0 + b/g,\ y_0 - a/g)
  3. (x0+a,y0+b)(x_0 + a, y_0 + b)
  4. (2x0,2y0)(2x_0, 2y_0)

With coins of 3 and 5, the largest amount that cannot be paid is…

  1. 7
  2. 8
  3. 15
  4. 1