Algorithm Search⌘K

Proofs: Extended Euclidean algorithm

These are the human proofs of Extended Euclidean algorithm, at full length.

The extended Euclidean algorithm returns gcd(a, b) and Bezout coefficients bounded by b/g and a/g full

This proof shows: correctness, termination, time.worst, bit_operations.worst.

We use the pseudocode EXTENDED-EUCLID of the page, with inputs a0,b0a_0, b_0 of any sign. Write a=∣a0∣a = |a_0| and b=∣b0∣b = |b_0|. Number the values that the pairs take: r0=ar_0 = a, r1=br_1 = b, and while ri≠0r_i \ne 0, qi=⌊ri−1/ri⌋q_i = \lfloor r_{i-1} / r_i \rfloor and

ri+1=ri−1−qiri,si+1=si−1−qisi,ti+1=ti−1−qiti,r_{i+1} = r_{i-1} - q_i r_i, \quad s_{i+1} = s_{i-1} - q_i s_i, \quad t_{i+1} = t_{i-1} - q_i t_i,

with s0=1,s1=0,t0=0,t1=1s_0 = 1, s_1 = 0, t_0 = 0, t_1 = 1. Pass ii of the loop turns the pairs (ri−1,ri)(r_{i-1}, r_i), (si−1,si)(s_{i-1}, s_i), (ti−1,ti)(t_{i-1}, t_i) into (ri,ri+1)(r_i, r_{i+1}), (si,si+1)(s_i, s_{i+1}), (ti,ti+1)(t_i, t_{i+1}). Let kk be the number of passes, so rk+1=0r_{k+1} = 0 and the loop returns from (rk,sk,tk)(r_k, s_k, t_k).

Lemma 1 (the remainder sequence). The values rir_i are those of the Euclidean algorithm on (a,b)(a, b). So the loop stops after k≤2+log⁡φmin⁡(a,b)k \le 2 + \log_\varphi \min(a, b) passes (for a,b≥1a, b \ge 1), and rk=gcd⁡(a,b)=gcd⁡(a0,b0)r_k = \gcd(a, b) = \gcd(a_0, b_0).

Proof. ri+1=ri−1−⌊ri−1/ri⌋ri=ri−1 mod rir_{i+1} = r_{i-1} - \lfloor r_{i-1}/r_i \rfloor r_i = r_{i-1} \bmod r_i, which is the step of the Euclidean algorithm. Its correctness and its step bound are proved on that page. □\square

Lemma 2 (the invariant). For each ii from 00 to k+1k + 1, asi+bti=ria s_i + b t_i = r_i.

Proof. For i=0i = 0: a⋅1+b⋅0=a=r0a \cdot 1 + b \cdot 0 = a = r_0. For i=1i = 1: a⋅0+b⋅1=r1a \cdot 0 + b \cdot 1 = r_1. If it holds for i−1i - 1 and ii, then asi+1+bti+1=(asi−1+bti−1)−qi(asi+bti)=ri−1−qiri=ri+1a s_{i+1} + b t_{i+1} = (a s_{i-1} + b t_{i-1}) - q_i (a s_i + b t_i) = r_{i-1} - q_i r_i = r_{i+1}. □\square

Theorem 1 (correctness). EXTENDED-EUCLID(a0,b0)(a_0, b_0) stops and returns (g,x,y)(g, x, y) with g=gcd⁡(a0,b0)≥0g = \gcd(a_0, b_0) \ge 0 and a0x+b0y=ga_0 x + b_0 y = g.

Proof. The loop stops by Lemma 1, with rk=gr_k = g. By Lemma 2, ask+btk=ga s_k + b t_k = g. Line L9 sets x=σaskx = \sigma_a s_k and y=σbtky = \sigma_b t_k, where σa=−1\sigma_a = -1 if a0<0a_0 < 0 and 11 otherwise, and likewise σb\sigma_b. Since a0=σaaa_0 = \sigma_a a and σa2=1\sigma_a^2 = 1, a0x=aska_0 x = a s_k, and likewise b0y=btkb_0 y = b t_k. So a0x+b0y=ga_0 x + b_0 y = g. If a0=b0=0a_0 = b_0 = 0, the loop does not run and the result is (0,1,0)(0, 1, 0), which satisfies 0⋅1+0⋅0=00 \cdot 1 + 0 \cdot 0 = 0. ■\blacksquare

Lemma 3 (determinant and signs). For 0≤i≤k0 \le i \le k:

  1. siti+1−si+1ti=(−1)is_i t_{i+1} - s_{i+1} t_i = (-1)^i.
  2. (−1)isi≥0(-1)^i s_i \ge 0 and (−1)i+1ti≥0(-1)^{i+1} t_i \ge 0; that is, the signs of the sis_i alternate (with zeros allowed), and so do the signs of the tit_i.

Proof. (1) For i=0i = 0: 1⋅1−0⋅0=11 \cdot 1 - 0 \cdot 0 = 1. Step: si+1ti+2−si+2ti+1=si+1(ti−qi+1ti+1)−(si−qi+1si+1)ti+1=si+1ti−siti+1=−(−1)is_{i+1} t_{i+2} - s_{i+2} t_{i+1} = s_{i+1}(t_i - q_{i+1} t_{i+1}) - (s_i - q_{i+1} s_{i+1}) t_{i+1} = s_{i+1} t_i - s_i t_{i+1} = -(-1)^i. (2) True for i=0,1i = 0, 1: s0=1s_0 = 1, s1=0s_1 = 0, t0=0t_0 = 0, t1=1t_1 = 1. If true for i−1i - 1 and ii, then, since qi≥0q_i \ge 0,

(−1)i+1si+1=(−1)i−1si−1+qi(−1)isi≥0,(-1)^{i+1} s_{i+1} = (-1)^{i-1} s_{i-1} + q_i (-1)^i s_i \ge 0,

and the same computation works for tt. □\square

Lemma 4 (two exact identities). For 0≤i≤k0 \le i \le k, if a,b≥1a, b \ge 1:

∣si∣ ri+1+∣si+1∣ ri=b,∣ti∣ ri+1+∣ti+1∣ ri=a.|s_i| \, r_{i+1} + |s_{i+1}| \, r_i = b, \qquad |t_i| \, r_{i+1} + |t_{i+1}| \, r_i = a.

Proof. By Lemma 2, siri+1−si+1ri=si(asi+1+bti+1)−si+1(asi+bti)=b(siti+1−si+1ti)=(−1)ibs_i r_{i+1} - s_{i+1} r_i = s_i (a s_{i+1} + b t_{i+1}) - s_{i+1}(a s_i + b t_i) = b (s_i t_{i+1} - s_{i+1} t_i) = (-1)^i b by Lemma 3 (1). Multiply by (−1)i(-1)^i. By Lemma 3 (2), (−1)isi=∣si∣(-1)^i s_i = |s_i| and −(−1)isi+1=(−1)i+1si+1=∣si+1∣-(-1)^i s_{i+1} = (-1)^{i+1} s_{i+1} = |s_{i+1}|, and the rr values are non-negative, so the left side is ∣si∣ri+1+∣si+1∣ri|s_i| r_{i+1} + |s_{i+1}| r_i. The identity for tt is the same computation: tiri+1−ti+1ri=a(tisi+1−ti+1si)=−(−1)iat_i r_{i+1} - t_{i+1} r_i = a(t_i s_{i+1} - t_{i+1} s_i) = -(-1)^i a. □\square

Theorem 2 (size of the coefficients). Let a,b≥1a, b \ge 1 and g=gcd⁡(a,b)g = \gcd(a, b).

  1. Each ∣si∣≤b/g|s_i| \le b/g and each ∣ti∣≤a/g|t_i| \le a/g, for 0≤i≤k+10 \le i \le k + 1. In particular ∣x∣≤b/g|x| \le b/g and ∣y∣≤a/g|y| \le a/g.
  2. ∣sk+1∣=b/g|s_{k+1}| = b/g and ∣tk+1∣=a/g|t_{k+1}| = a/g.
  3. If b∤ab \nmid a, then ∣x∣≤b/(2g)|x| \le b/(2g) and ∣y∣≤a/(2g)|y| \le a/(2g). If b∣ab \mid a, then (x,y)=(0,±1)(x, y) = (0, \pm 1).
  4. qi∣si∣≤b/gq_i |s_i| \le b/g and qi∣ti∣≤a/gq_i |t_i| \le a/g for 1≤i≤k1 \le i \le k, so no intermediate product is larger than the inputs.

Proof. Each rir_i with i≤ki \le k is a positive multiple of gg, so ri≥gr_i \ge g. (1) For 1≤j≤k+11 \le j \le k + 1, Lemma 4 with i=j−1i = j - 1 gives ∣sj∣rj−1≤b|s_j| r_{j-1} \le b, so ∣sj∣≤b/g|s_j| \le b/g; and ∣s0∣=1≤b/g|s_0| = 1 \le b/g because g∣bg \mid b. The same holds for tt. (2) Lemma 4 with i=ki = k, where rk+1=0r_{k+1} = 0 and rk=gr_k = g, gives ∣sk+1∣g=b|s_{k+1}| g = b and ∣tk+1∣g=a|t_{k+1}| g = a. (3) If b∣ab \mid a, then k=1k = 1 and the result is (s1,t1)=(0,1)(s_1, t_1) = (0, 1), up to the signs of line L9. Otherwise k≥2k \ge 2, because r2=a mod b≠0r_2 = a \bmod b \ne 0. Then rk−1>rk=gr_{k-1} > r_k = g, since the remainders r1>r2>⋯>rkr_1 > r_2 > \cdots > r_k strictly decrease, and rk−1r_{k-1} is a multiple of gg, so rk−1≥2gr_{k-1} \ge 2g. Lemma 4 with i=k−1i = k - 1 gives ∣sk∣rk−1≤b|s_k| r_{k-1} \le b, so ∣sk∣≤b/(2g)|s_k| \le b/(2g), and likewise ∣tk∣≤a/(2g)|t_k| \le a/(2g). (4) By Lemma 3 (2), si−1s_{i-1} and −qisi-q_i s_i have the same sign or are zero, so ∣si+1∣=∣si−1∣+qi∣si∣≥qi∣si∣|s_{i+1}| = |s_{i-1}| + q_i |s_i| \ge q_i |s_i|, and ∣si+1∣≤b/g|s_{i+1}| \le b/g by (1). The same holds for tt. ■\blacksquare

Theorem 3 (cost). On the word RAM, if ∣a0∣|a_0| and ∣b0∣|b_0| fit in a signed word, the algorithm uses Θ(log⁡m)\Theta(\log m) word operations in the worst case, where m=min⁡(∣a0∣,∣b0∣)m = \min(|a_0|, |b_0|), and no value overflows. On inputs of at most nn binary digits it uses O(n2)O(n^2) bit operations.

Proof. Each pass does one division and three multiply-subtract steps, and by Lemma 1 there are O(log⁡m)O(\log m) passes; the Fibonacci inputs of the Euclidean algorithm show the bound is reached. By Theorem 2 (1) and (4), every sis_i, tit_i and product qisiq_i s_i, qitiq_i t_i has absolute value at most max⁡(a,b)\max(a, b), so it fits in the word. In the bit model, the rir_i part costs O(n2)O(n^2), as proved for the Euclidean algorithm. Each ∣si∣,∣ti∣<2n|s_i|, |t_i| < 2^n by Theorem 2, so the products qisiq_i s_i and qitiq_i t_i cost O((1+lg⁡qi)n)O((1 + \lg q_i) n) bit operations each with schoolbook multiplication, and the subtractions O(n)O(n). Summed over the passes this is O(n(k+∑ilg⁡qi))O(n (k + \sum_i \lg q_i)). As on the Euclidean algorithm page, q1q2⋯qk≤aq_1 q_2 \cdots q_k \le a when a≥ba \ge b (with one extra pass with q1=0q_1 = 0 when a<ba < b), so ∑lg⁡qi≤n\sum \lg q_i \le n, and k=O(n)k = O(n). The total is O(n2)O(n^2). ■\blacksquare

Back to Extended Euclidean algorithm