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≥1 be integers of at most n binary digits. With schoolbook long division, EUCLID(a,b) uses O(n2) bit operations.
Setup. Write the remainder sequence as r0=a, r1=b, and ri+1=ri−1modri, with quotients qi=⌊ri−1/ri⌋ for i=1,…,k, where rk+1=0 and rk=gcd(a,b)≥1. Iteration i divides ri−1 by ri. Schoolbook long division of ri−1 by ri produces the quotient one binary digit at a time, with one subtraction of at most lgri+1 digits for each digit of the quotient. The quotient has at most lgqi+1 digits, so iteration i costs at most c(1+lgqi)(1+lgri) bit operations for a constant c. (The case qi=0 happens only when ri−1<ri, which does not occur here because a≥b and each later remainder is smaller than its divisor.)
Step 1: the quotients multiply to at most a. For each i, ri−1=qiri+ri+1≥qiri. Chaining these inequalities gives a=r0≥q1r1≥q1q2r2≥⋯≥q1q2⋯qkrk≥q1q2⋯qk, since rk≥1. So ∑i=1klgqi≤lga≤n.
Step 2: the number of iterations. By Theorem 2 of the correctness proof, k≤1+logφb≤1+1.45n.
Step 3: the sum. Each ri≤b<2n, so 1+lgri≤n+1. Then
The moves of lines L4 and L5 copy at most n+1 digits each, and there are k=O(n) of them, so they add O(n2). The absolute values of line L1 cost O(n). ■
Remark. The same sum shows why the bound is not O(n3), which the product "O(n) iterations times O(n2) 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,b0 are any integers. We write gcd(x,y) for the greatest common divisor of x and y, with gcd(x,0)=∣x∣ and gcd(0,0)=0.
Lemma 1 (the step keeps the gcd). Let a≥0 and b>0 be integers, and let r=amodb. Then gcd(a,b)=gcd(b,r).
Proof. Write a=qb+r with q=⌊a/b⌋. If d∣a and d∣b, then d∣a−qb=r. If d∣b and d∣r, then d∣qb+r=a. So the pairs (a,b) and (b,r) have the same set of common divisors. Both pairs have a non-zero member (b>0), so the set is finite and its largest element is the gcd of each pair. □
Theorem 1 (correctness). EUCLID(a0,b0) stops and returns gcd(a0,b0).
Proof. After line L1, a=∣a0∣ and b=∣b0∣. Since d∣x if and only if d∣∣x∣, gcd(a,b)=gcd(a0,b0). We use this invariant of the loop of line L2:
(G)a≥0, b≥0, and gcd(a,b)=gcd(a0,b0).
Initialization. True after line L1, as shown above.
Maintenance. Assume (G) and b=0, so b>0. Line L3 computes r=amodb, with 0≤r<b. Lines L4 and L5 make the new pair (a′,b′)=(b,r). Both are non-negative, and by Lemma 1, gcd(a′,b′)=gcd(a,b)=gcd(a0,b0).
Termination. The value b is a non-negative integer, and each iteration replaces it by r≤b−1. So it drops by at least 1 at each iteration, and the loop runs at most ∣b0∣ times before b=0.
Conclusion. At exit b=0, and (G) gives gcd(a0,b0)=gcd(a,0)=a, because a≥0. Line L6 returns a. ■
Lemma 2 (Fibonacci bound). Let F0=0, F1=1, Fj+2=Fj+1+Fj. If a>b≥1 and the loop runs k≥1 times on (a,b), then b≥Fk+1 and a≥Fk+2.
Proof. By induction on k. Base k=1: b≥1=F2, and a>b≥1 gives a≥2=F3. Step: let k≥2 and assume the lemma for k−1. The first iteration turns (a,b) into (b,r) with r=amodb<b. The loop runs k−1≥1 more times, so r=0, and b>r≥1. By the induction hypothesis applied to (b,r), r≥Fk and b≥Fk+1. Since a>b, the quotient q=⌊a/b⌋ is at least 1, so a=qb+r≥b+r≥Fk+1+Fk=Fk+2. □
Theorem 2 (Lamé's bound). If a≥b≥1, the loop runs at most 1+logφb times, where φ=(1+5)/2. If 0≤a<b, it runs at most 2+logφa times when a≥1, once when a=0, and not at all when b=0.
Proof. Let the loop run k times on a≥b≥1. If a=b, then amodb=0 and k=1≤1+logφb. If a>b, Lemma 2 gives b≥Fk+1. We show Fj≥φj−2 for j≥1 by induction. F1=1≥φ−1 and F2=1=φ0. For j≥3, Fj=Fj−1+Fj−2≥φj−3+φj−4=φj−4(φ+1)=φj−2, since φ2=φ+1. So b≥Fk+1≥φk−1, which gives k≤1+logφb. If 0<a<b, the first iteration gives (b,a) with b>a≥1, and the rest runs at most 1+logφa times. If a=0<b, the first iteration gives (b,0) and the loop stops. ■
Tightness. For k≥1, the input (Fk+2,Fk+1) makes the loop run exactly k times. For j≥3, Fj+1=1⋅Fj+Fj−1 with 0≤Fj−1<Fj, so Fj+1modFj=Fj−1 and one iteration maps (Fj+1,Fj) to (Fj,Fj−1). Starting from j=k+1, after k−1 iterations the pair is (F3,F2)=(2,1), and one more iteration gives (1,0). So the bound Θ(logb) is reached.