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,b0 of any sign. Write a=∣a0∣ and b=∣b0∣. Number the values that the pairs take: r0=a, r1=b, and while ri=0, qi=⌊ri−1/ri⌋ and
ri+1=ri−1−qiri,si+1=si−1−qisi,ti+1=ti−1−qiti, with s0=1,s1=0,t0=0,t1=1. Pass i of the loop turns the pairs (ri−1,ri), (si−1,si), (ti−1,ti) into (ri,ri+1), (si,si+1), (ti,ti+1). Let k be the number of passes, so rk+1=0 and the loop returns from (rk,sk,tk).
Lemma 1 (the remainder sequence). The values ri are those of the Euclidean algorithm on (a,b). So the loop stops after k≤2+logφmin(a,b) passes (for a,b≥1), and rk=gcd(a,b)=gcd(a0,b0).
Proof. ri+1=ri−1−⌊ri−1/ri⌋ri=ri−1modri, which is the step of the Euclidean algorithm. Its correctness and its step bound are proved on that page. □
Lemma 2 (the invariant). For each i from 0 to k+1, asi+bti=ri.
Proof. For i=0: a⋅1+b⋅0=a=r0. For i=1: a⋅0+b⋅1=r1. If it holds for i−1 and i, then asi+1+bti+1=(asi−1+bti−1)−qi(asi+bti)=ri−1−qiri=ri+1. □
Theorem 1 (correctness). EXTENDED-EUCLID(a0,b0) stops and returns (g,x,y) with g=gcd(a0,b0)≥0 and a0x+b0y=g.
Proof. The loop stops by Lemma 1, with rk=g. By Lemma 2, ask+btk=g. Line L9 sets x=σask and y=σbtk, where σa=−1 if a0<0 and 1 otherwise, and likewise σb. Since a0=σaa and σa2=1, a0x=ask, and likewise b0y=btk. So a0x+b0y=g. If a0=b0=0, the loop does not run and the result is (0,1,0), which satisfies 0⋅1+0⋅0=0. ■
Lemma 3 (determinant and signs). For 0≤i≤k:
- siti+1−si+1ti=(−1)i.
- (−1)isi≥0 and (−1)i+1ti≥0; that is, the signs of the si alternate (with zeros allowed), and so do the signs of the ti.
Proof. (1) For i=0: 1⋅1−0⋅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)i. (2) True for i=0,1: s0=1, s1=0, t0=0, t1=1. If true for i−1 and i, then, since qi≥0,
(−1)i+1si+1=(−1)i−1si−1+qi(−1)isi≥0, and the same computation works for t. □
Lemma 4 (two exact identities). For 0≤i≤k, if a,b≥1:
∣si∣ri+1+∣si+1∣ri=b,∣ti∣ri+1+∣ti+1∣ri=a. Proof. By Lemma 2, siri+1−si+1ri=si(asi+1+bti+1)−si+1(asi+bti)=b(siti+1−si+1ti)=(−1)ib by Lemma 3 (1). Multiply by (−1)i. By Lemma 3 (2), (−1)isi=∣si∣ and −(−1)isi+1=(−1)i+1si+1=∣si+1∣, and the r values are non-negative, so the left side is ∣si∣ri+1+∣si+1∣ri. The identity for t is the same computation: tiri+1−ti+1ri=a(tisi+1−ti+1si)=−(−1)ia. □
Theorem 2 (size of the coefficients). Let a,b≥1 and g=gcd(a,b).
- Each ∣si∣≤b/g and each ∣ti∣≤a/g, for 0≤i≤k+1. In particular ∣x∣≤b/g and ∣y∣≤a/g.
- ∣sk+1∣=b/g and ∣tk+1∣=a/g.
- If b∤a, then ∣x∣≤b/(2g) and ∣y∣≤a/(2g). If b∣a, then (x,y)=(0,±1).
- qi∣si∣≤b/g and qi∣ti∣≤a/g for 1≤i≤k, so no intermediate product is larger than the inputs.
Proof. Each ri with i≤k is a positive multiple of g, so ri≥g. (1) For 1≤j≤k+1, Lemma 4 with i=j−1 gives ∣sj∣rj−1≤b, so ∣sj∣≤b/g; and ∣s0∣=1≤b/g because g∣b. The same holds for t. (2) Lemma 4 with i=k, where rk+1=0 and rk=g, gives ∣sk+1∣g=b and ∣tk+1∣g=a. (3) If b∣a, then k=1 and the result is (s1,t1)=(0,1), up to the signs of line L9. Otherwise k≥2, because r2=amodb=0. Then rk−1>rk=g, since the remainders r1>r2>⋯>rk strictly decrease, and rk−1 is a multiple of g, so rk−1≥2g. Lemma 4 with i=k−1 gives ∣sk∣rk−1≤b, so ∣sk∣≤b/(2g), and likewise ∣tk∣≤a/(2g). (4) By Lemma 3 (2), si−1 and −qisi have the same sign or are zero, so ∣si+1∣=∣si−1∣+qi∣si∣≥qi∣si∣, and ∣si+1∣≤b/g by (1). The same holds for t. ■
Theorem 3 (cost). On the word RAM, if ∣a0∣ and ∣b0∣ fit in a signed word, the algorithm uses Θ(logm) word operations in the worst case, where m=min(∣a0∣,∣b0∣), and no value overflows. On inputs of at most n binary digits it uses O(n2) bit operations.
Proof. Each pass does one division and three multiply-subtract steps, and by Lemma 1 there are O(logm) passes; the Fibonacci inputs of the Euclidean algorithm show the bound is reached. By Theorem 2 (1) and (4), every si, ti and product qisi, qiti has absolute value at most max(a,b), so it fits in the word. In the bit model, the ri part costs O(n2), as proved for the Euclidean algorithm. Each ∣si∣,∣ti∣<2n by Theorem 2, so the products qisi and qiti cost O((1+lgqi)n) bit operations each with schoolbook multiplication, and the subtractions O(n). Summed over the passes this is O(n(k+∑ilgqi)). As on the Euclidean algorithm page, q1q2⋯qk≤a when a≥b (with one extra pass with q1=0 when a<b), so ∑lgqi≤n, and k=O(n). The total is O(n2). ■