Algorithm Search⌘K

Proofs: Euclidean algorithm

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

The Euclidean algorithm uses O(n^2) bit operations on n-bit inputs full

This proof shows: bit_operations.worst.

Claim. Let a≥b≥1a \ge b \ge 1 be integers of at most nn binary digits. With schoolbook long division, EUCLID(a,b)(a, b) uses O(n2)O(n^2) bit operations.

Setup. Write the remainder sequence as r0=ar_0 = a, r1=br_1 = b, and ri+1=ri−1 mod rir_{i+1} = r_{i-1} \bmod r_i, with quotients qi=⌊ri−1/ri⌋q_i = \lfloor r_{i-1} / r_i \rfloor for i=1,…,ki = 1, \ldots, k, where rk+1=0r_{k+1} = 0 and rk=gcd⁡(a,b)≥1r_k = \gcd(a, b) \ge 1. Iteration ii divides ri−1r_{i-1} by rir_i. Schoolbook long division of ri−1r_{i-1} by rir_i produces the quotient one binary digit at a time, with one subtraction of at most lg⁡ri+1\lg r_i + 1 digits for each digit of the quotient. The quotient has at most lg⁡qi+1\lg q_i + 1 digits, so iteration ii costs at most c(1+lg⁡qi)(1+lg⁡ri)c (1 + \lg q_i)(1 + \lg r_i) bit operations for a constant cc. (The case qi=0q_i = 0 happens only when ri−1<rir_{i-1} < r_i, which does not occur here because a≥ba \ge b and each later remainder is smaller than its divisor.)

Step 1: the quotients multiply to at most aa. For each ii, ri−1=qiri+ri+1≥qirir_{i-1} = q_i r_i + r_{i+1} \ge q_i r_i. Chaining these inequalities gives a=r0≥q1r1≥q1q2r2≥⋯≥q1q2⋯qkrk≥q1q2⋯qka = r_0 \ge q_1 r_1 \ge q_1 q_2 r_2 \ge \cdots \ge q_1 q_2 \cdots q_k r_k \ge q_1 q_2 \cdots q_k, since rk≥1r_k \ge 1. So ∑i=1klg⁡qi≤lg⁡a≤n\sum_{i=1}^{k} \lg q_i \le \lg a \le n.

Step 2: the number of iterations. By Theorem 2 of the correctness proof, k≤1+log⁡φb≤1+1.45nk \le 1 + \log_\varphi b \le 1 + 1.45 n.

Step 3: the sum. Each ri≤b<2nr_i \le b < 2^n, so 1+lg⁡ri≤n+11 + \lg r_i \le n + 1. Then

∑i=1kc(1+lg⁡qi)(1+lg⁡ri)≤c(n+1)(k+∑i=1klg⁡qi)≤c(n+1)(1+1.45n+n)=O(n2).\sum_{i=1}^{k} c(1 + \lg q_i)(1 + \lg r_i) \le c(n + 1)\Big(k + \sum_{i=1}^{k} \lg q_i\Big) \le c(n + 1)(1 + 1.45n + n) = O(n^2).

The moves of lines L4 and L5 copy at most n+1n + 1 digits each, and there are k=O(n)k = O(n) of them, so they add O(n2)O(n^2). The absolute values of line L1 cost O(n)O(n). ■\blacksquare

Remark. The same sum shows why the bound is not O(n3)O(n^3), which the product "O(n)O(n) iterations times O(n2)O(n^2) for each division" would suggest: a division is expensive only when its quotient is large, and large quotients make the remainders shrink fast.

The Euclidean algorithm returns gcd(a, b) and stops after at most 1 + log_phi b steps full

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

We use the pseudocode EUCLID of the page. The inputs a0,b0a_0, b_0 are any integers. We write gcd⁡(x,y)\gcd(x, y) for the greatest common divisor of xx and yy, with gcd⁡(x,0)=∣x∣\gcd(x, 0) = |x| and gcd⁡(0,0)=0\gcd(0, 0) = 0.

Lemma 1 (the step keeps the gcd). Let a≥0a \ge 0 and b>0b > 0 be integers, and let r=a mod br = a \bmod b. Then gcd⁡(a,b)=gcd⁡(b,r)\gcd(a, b) = \gcd(b, r).

Proof. Write a=qb+ra = qb + r with q=⌊a/b⌋q = \lfloor a/b \rfloor. If d∣ad \mid a and d∣bd \mid b, then d∣a−qb=rd \mid a - qb = r. If d∣bd \mid b and d∣rd \mid r, then d∣qb+r=ad \mid qb + r = a. So the pairs (a,b)(a, b) and (b,r)(b, r) have the same set of common divisors. Both pairs have a non-zero member (b>0b > 0), so the set is finite and its largest element is the gcd of each pair. □\square

Theorem 1 (correctness). EUCLID(a0,b0)(a_0, b_0) stops and returns gcd⁡(a0,b0)\gcd(a_0, b_0).

Proof. After line L1, a=∣a0∣a = |a_0| and b=∣b0∣b = |b_0|. Since d∣xd \mid x if and only if d∣∣x∣d \mid |x|, gcd⁡(a,b)=gcd⁡(a0,b0)\gcd(a, b) = \gcd(a_0, b_0). We use this invariant of the loop of line L2:

(G) a≥0a \ge 0, b≥0b \ge 0, and gcd⁡(a,b)=gcd⁡(a0,b0)\gcd(a, b) = \gcd(a_0, b_0).

Initialization. True after line L1, as shown above.

Maintenance. Assume (G) and b≠0b \ne 0, so b>0b > 0. Line L3 computes r=a mod br = a \bmod b, with 0≤r<b0 \le r < b. Lines L4 and L5 make the new pair (a′,b′)=(b,r)(a', b') = (b, r). Both are non-negative, and by Lemma 1, gcd⁡(a′,b′)=gcd⁡(a,b)=gcd⁡(a0,b0)\gcd(a', b') = \gcd(a, b) = \gcd(a_0, b_0).

Termination. The value bb is a non-negative integer, and each iteration replaces it by r≤b−1r \le b - 1. So it drops by at least 11 at each iteration, and the loop runs at most ∣b0∣|b_0| times before b=0b = 0.

Conclusion. At exit b=0b = 0, and (G) gives gcd⁡(a0,b0)=gcd⁡(a,0)=a\gcd(a_0, b_0) = \gcd(a, 0) = a, because a≥0a \ge 0. Line L6 returns aa. ■\blacksquare

Lemma 2 (Fibonacci bound). Let F0=0F_0 = 0, F1=1F_1 = 1, Fj+2=Fj+1+FjF_{j+2} = F_{j+1} + F_j. If a>b≥1a > b \ge 1 and the loop runs k≥1k \ge 1 times on (a,b)(a, b), then b≥Fk+1b \ge F_{k+1} and a≥Fk+2a \ge F_{k+2}.

Proof. By induction on kk. Base k=1k = 1: b≥1=F2b \ge 1 = F_2, and a>b≥1a > b \ge 1 gives a≥2=F3a \ge 2 = F_3. Step: let k≥2k \ge 2 and assume the lemma for k−1k - 1. The first iteration turns (a,b)(a, b) into (b,r)(b, r) with r=a mod b<br = a \bmod b < b. The loop runs k−1≥1k - 1 \ge 1 more times, so r≠0r \ne 0, and b>r≥1b > r \ge 1. By the induction hypothesis applied to (b,r)(b, r), r≥Fkr \ge F_k and b≥Fk+1b \ge F_{k+1}. Since a>ba > b, the quotient q=⌊a/b⌋q = \lfloor a/b \rfloor is at least 11, so a=qb+r≥b+r≥Fk+1+Fk=Fk+2a = qb + r \ge b + r \ge F_{k+1} + F_k = F_{k+2}. □\square

Theorem 2 (Lamé's bound). If a≥b≥1a \ge b \ge 1, the loop runs at most 1+log⁡φb1 + \log_\varphi b times, where φ=(1+5)/2\varphi = (1 + \sqrt 5)/2. If 0≤a<b0 \le a < b, it runs at most 2+log⁡φa2 + \log_\varphi a times when a≥1a \ge 1, once when a=0a = 0, and not at all when b=0b = 0.

Proof. Let the loop run kk times on a≥b≥1a \ge b \ge 1. If a=ba = b, then a mod b=0a \bmod b = 0 and k=1≤1+log⁡φbk = 1 \le 1 + \log_\varphi b. If a>ba > b, Lemma 2 gives b≥Fk+1b \ge F_{k+1}. We show Fj≥φj−2F_{j} \ge \varphi^{j-2} for j≥1j \ge 1 by induction. F1=1≥φ−1F_1 = 1 \ge \varphi^{-1} and F2=1=φ0F_2 = 1 = \varphi^0. For j≥3j \ge 3, Fj=Fj−1+Fj−2≥φj−3+φj−4=φj−4(φ+1)=φj−2F_j = F_{j-1} + F_{j-2} \ge \varphi^{j-3} + \varphi^{j-4} = \varphi^{j-4}(\varphi + 1) = \varphi^{j-2}, since φ2=φ+1\varphi^2 = \varphi + 1. So b≥Fk+1≥φk−1b \ge F_{k+1} \ge \varphi^{k-1}, which gives k≤1+log⁡φbk \le 1 + \log_\varphi b. If 0<a<b0 < a < b, the first iteration gives (b,a)(b, a) with b>a≥1b > a \ge 1, and the rest runs at most 1+log⁡φa1 + \log_\varphi a times. If a=0<ba = 0 < b, the first iteration gives (b,0)(b, 0) and the loop stops. ■\blacksquare

Tightness. For k≥1k \ge 1, the input (Fk+2,Fk+1)(F_{k+2}, F_{k+1}) makes the loop run exactly kk times. For j≥3j \ge 3, Fj+1=1⋅Fj+Fj−1F_{j+1} = 1 \cdot F_j + F_{j-1} with 0≤Fj−1<Fj0 \le F_{j-1} < F_j, so Fj+1 mod Fj=Fj−1F_{j+1} \bmod F_j = F_{j-1} and one iteration maps (Fj+1,Fj)(F_{j+1}, F_j) to (Fj,Fj−1)(F_j, F_{j-1}). Starting from j=k+1j = k + 1, after k−1k - 1 iterations the pair is (F3,F2)=(2,1)(F_3, F_2) = (2, 1), and one more iteration gives (1,0)(1, 0). So the bound Θ(log⁡b)\Theta(\log b) is reached.

Back to Euclidean algorithm