Key fact (m ≥ n): gcd(m, n) = gcd(n, m − n).
Why: if d divides m = ad and n = bd, then m − n = (a − b)d, so d divides m − n. Conversely, if d divides n and m − n, it divides their sum m. So both pairs have the same common divisors. In pictures: if blocks of length d tile m and n, they also tile m − n.
Euclid's algorithm for gcd(m, n):
- If m < n, swap them.
- If n = 0, the answer is m.
- Otherwise, reduce to gcd(n, m − n).
gcd(375, 825): swap → (825, 375) → (375, 450) → swap (450, 375) → (375, 75) → (75, 300) → (75, 225) → (75, 150) → (75, 75) → (75, 0) → 75. That is 7 reductions.
The catch: gcd(99, 2) goes (97, 2), (95, 2), …, (3, 2), (2, 1), (1, 1), (1, 0): about 49 reductions. For gcd(2k + 1, 2) it takes about k steps, so the work grows with the size of the number, not its number of digits.
Euclid wrote this down in his book Elements; historians think earlier Greek mathematicians knew it.
Tap the diagram to enlarge it
- Swap if m < n
- Stop at gcd(m, 0) = m
- gcd(99, 2) is slow by subtraction