The key identity is a one-line proof
Any common divisor of a and b divides a − qb, and conversely. So the two pairs have the same common divisors, hence the same greatest one.
One identity — gcd(a,b) = gcd(b, a mod b) — repeated. It terminates quickly, provably: consecutive Fibonacci numbers are the worst case and the number of steps is logarithmic.
Core ideas
The algorithm is two lines. The interesting parts are why it is correct and why it is fast.
Any common divisor of a and b divides a − qb, and conversely. So the two pairs have the same common divisors, hence the same greatest one.
Each remainder is strictly smaller than the previous divisor and non-negative, so the sequence must reach 0.
Two steps at least halve the larger number, so the count is logarithmic. The worst case is consecutive Fibonacci numbers.
The material
The identity, the correctness proof, and the running-time analysis.
For b > 0, gcd(a, b) = gcd(b, a mod b).
Write a = qb + r with r = a mod b. If d divides a and b, then it divides a − qb = r, so it is a common divisor of b and r.
Conversely if d divides b and r, then it divides qb + r = a, so it is a common divisor of a and b.
The two pairs therefore have exactly the same common divisors, so the greatest is the same.
□
Correctness is the loop invariant of “Loop Invariants and Program Correctness” in Induction and Recursion: gcd(a, b) never changes, by the identity. Termination holds because b strictly decreases and is a non-negative integer. At exit b = 0 and gcd(a, 0) = a.
In two iterations, the first argument is at least halved.
If b ≤ a/2 then a mod b < b ≤ a/2 and one step suffices. Otherwise b > a/2, so a = 1·b + (a − b) and a mod b = a − b < a/2.
Either way the value two steps later is below a/2, so the number of iterations is O(log a).
□
The number of division steps is at most five times the number of decimal digits of the smaller input, and the worst case occurs exactly for consecutive Fibonacci numbers.
Fibonacci inputs are worst because every quotient is 1 — the slowest possible progress. gcd(F_{n+1}, F_n) takes n − 1 steps, and since F_n grows like φⁿ, that is logarithmic in the input. “Solving Linear Recurrences” in Induction and Recursion supplied the growth rate.
By contrast gcd(1071, 462) takes three steps despite the numbers being much larger. Size is not what determines the step count; the pattern of quotients is.
Euclid's algorithm computes gcds of thousand-digit numbers in milliseconds and is used inside every implementation of RSA, in reducing fractions, and in computer algebra generally. It is among the oldest algorithms still in daily use, and it has not been improved on asymptotically for the general case.
Interactive
Algorithms with their traces
Method
Divide, keep the remainder, repeat.
Record the quotient and the remainder. The quotient is not needed for the gcd but is needed for “Bézout's Identity and Modular Inverses”.
(a, b) becomes (b, r). The gcd is unchanged, by the identity.
The remainders strictly decrease, so this happens.
That is the gcd. Equivalently, it is the value of a when b reaches 0.
Three divisions, and a check.
Three divisions against the eight or so needed to factor both numbers — and the gap widens without limit as the inputs grow. That asymmetry is the practical content of “Greatest Common Divisor”'s warning.
Run the algorithm on gcd(144, 89) and count the divisions before starting: 144 = F₁₂ and 89 = F₁₁, so the body's rule names the number. Consecutive Fibonacci numbers give the maximum, and knowing that in advance is the analysis working.
Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.
No recommendations yet for this lesson.