Algorithm Search⌘K

Proofs: Finding a solution of a linear Diophantine equation

These are the human proofs of Finding a solution of a linear Diophantine equation, at full length.

LINEAR-DIOPHANTINE returns a solution exactly when one exists full

This proof shows: correctness.

Let aa, bb and cc be integers. We write A=∣a∣A = |a| and B=∣b∣B = |b|, and σa\sigma_a, σb\sigma_b for the signs used in line L11, so that aσa=Aa \sigma_a = A and bσb=Bb \sigma_b = B. We use gcd⁡(0,0)=0\gcd(0, 0) = 0, and gcd⁡(u,v)=gcd⁡(∣u∣,∣v∣)\gcd(u, v) = \gcd(|u|, |v|).

Lemma 1 (necessity). If ax+by=cax + by = c for some integers xx and yy, then gcd⁡(a,b)∣c\gcd(a, b) \mid c.

Proof. g=gcd⁡(a,b)g = \gcd(a, b) divides aa and bb, so it divides ax+byax + by. If g=0g = 0 then a=b=0a = b = 0 and c=0=g⋅0c = 0 = g \cdot 0. □\square

Lemma 2 (the loop). Assume (a,b)≠(0,0)(a, b) \ne (0, 0). Before each test of line L4, the following invariant holds:

(E) r0=As0+Bt0r_0 = A s_0 + B t_0, r1=As1+Bt1r_1 = A s_1 + B t_1, r0≥0r_0 \ge 0, r1≥0r_1 \ge 0, and gcd⁡(r0,r1)=gcd⁡(a,b)\gcd(r_0, r_1) = \gcd(a, b).

The loop stops, and after it r1=0r_1 = 0 and g=r0=gcd⁡(a,b)≥1g = r_0 = \gcd(a, b) \ge 1.

Proof. Initialization. Line L3 sets (r0,s0,t0)=(A,1,0)(r_0, s_0, t_0) = (A, 1, 0) and (r1,s1,t1)=(B,0,1)(r_1, s_1, t_1) = (B, 0, 1). Then A=A⋅1+B⋅0A = A \cdot 1 + B \cdot 0, B=A⋅0+B⋅1B = A \cdot 0 + B \cdot 1, both are nonnegative, and gcd⁡(A,B)=gcd⁡(a,b)\gcd(A, B) = \gcd(a, b).

Maintenance. Assume (E) and r1≠0r_1 \ne 0, so r1>0r_1 > 0. Line L5 sets q=⌊r0/r1⌋q = \lfloor r_0 / r_1 \rfloor, and the new values are r0′=r1r_0' = r_1, r1′=r0−qr1r_1' = r_0 - q r_1, s0′=s1s_0' = s_1, s1′=s0−qs1s_1' = s_0 - q s_1, t0′=t1t_0' = t_1, t1′=t0−qt1t_1' = t_0 - q t_1. The first relation for the new values is the old second relation. For the second,

As1′+Bt1′=(As0+Bt0)−q(As1+Bt1)=r0−qr1=r1′.A s_1' + B t_1' = (A s_0 + B t_0) - q (A s_1 + B t_1) = r_0 - q r_1 = r_1'.

Also r0′=r1>0r_0' = r_1 > 0 and r1′=r0 mod r1∈[0,r1)r_1' = r_0 \bmod r_1 \in [0, r_1). By the GCD recursion theorem, gcd⁡(r1,r0−qr1)=gcd⁡(r0,r1)=gcd⁡(a,b)\gcd(r_1, r_0 - q r_1) = \gcd(r_0, r_1) = \gcd(a, b). So (E) holds again.

Termination. In each iteration the new r1r_1 is r0 mod r1<r1r_0 \bmod r_1 < r_1. So r1r_1 is a nonnegative integer that strictly decreases, and the loop stops. When it stops, r1=0r_1 = 0, and (E) gives gcd⁡(r0,0)=r0=gcd⁡(a,b)\gcd(r_0, 0) = r_0 = \gcd(a, b). Since (a,b)≠(0,0)(a, b) \ne (0, 0), gcd⁡(a,b)≥1\gcd(a, b) \ge 1. Line L9 sets g=r0g = r_0. □\square

Theorem. LINEAR-DIOPHANTINE(a,b,c)(a, b, c) returns a pair (x,y)(x, y) with ax+by=cax + by = c if a solution exists, and NONE otherwise.

Proof. Case a=b=0a = b = 0. Then ax+by=0ax + by = 0 for all xx and yy, so a solution exists if and only if c=0c = 0. Lines L1 and L2 return (0,0)(0, 0), which is a solution, when c=0c = 0, and NONE otherwise.

Case (a,b)≠(0,0)(a, b) \ne (0, 0). By Lemma 2, line L9 sets g=gcd⁡(a,b)≥1g = \gcd(a, b) \ge 1, and As0+Bt0=gA s_0 + B t_0 = g. If g∤cg \nmid c, then by Lemma 1 there is no solution, and line L10 returns NONE. If g∣cg \mid c, let m=c/gm = c / g, an integer. Line L11 sets x=σas0mx = \sigma_a s_0 m and y=σbt0my = \sigma_b t_0 m, so

ax+by=(aσa)s0m+(bσb)t0m=(As0+Bt0) m=gm=c,ax + by = (a \sigma_a) s_0 m + (b \sigma_b) t_0 m = (A s_0 + B t_0)\, m = g m = c,

and line L12 returns this solution. ■\blacksquare

The step. In the case (a,b)≠(0,0)(a, b) \ne (0, 0), the vector (s1,t1)(s_1, t_1) at the end satisfies As1+Bt1=r1=0A s_1 + B t_1 = r_1 = 0, and it is nonzero, because the matrix with rows (s0,t0)(s_0, t_0) and (s1,t1)(s_1, t_1) starts as the identity and each iteration multiplies it on the left by a matrix of determinant −1-1, so its determinant stays ±1\pm 1. Hence s0t1−s1t0=±1s_0 t_1 - s_1 t_0 = \pm 1, which forces gcd⁡(s1,t1)=1\gcd(s_1, t_1) = 1. From As1=−Bt1A s_1 = -B t_1 with gcd⁡(s1,t1)=1\gcd(s_1, t_1) = 1 it follows that (s1,t1)=±(B/g,−A/g)(s_1, t_1) = \pm (B/g, -A/g), the step between consecutive solutions of the equation with ∣a∣|a| and ∣b∣|b|.

Back to Finding a solution of a linear Diophantine equation