Euclid's Algorithm
What is the biggest number that divides both 1071 and 462? You could list all their divisors… or you could cut a rectangle into squares.
- The greatest common divisor (GCD) of two numbers is the biggest number that divides both of them. The GCD of 12 and 18 is 6.
- The remainder is what's left over after dividing: 17 ÷ 5 is 3, remainder 2, because 17 = 3 × 5 + 2.
- Two numbers are coprime when their GCD is 1: no number bigger than 1 divides them both.
- Divide the bigger number by the smaller one, and keep the remainder.
- Throw away the bigger number. Your new pair is the smaller number and the remainder.
- Repeat until the remainder is 0. The last number you divided by is the GCD.
In the picture: a rectangle that is a wide and b tall. Cut off as many b × b squares as fit. The strip left over is a new, smaller rectangle. Keep going. The last squares tile it perfectly, with nothing left: their side is the GCD.
- Why must the smallest square divide both a and b? (Hint: look at how the bigger squares are built from smaller ones.)
- Try 13 and 8, then 21 and 13 (press Fibonacci pair). Why do these take so many steps for such small numbers?
- Try two different primes. What's their GCD, every time? Why?
Curator's answers
- Each strip is tiled by the squares cut from it. Working backward, every square is made of whole smaller squares, so the smallest square fits into each side of the rectangle a whole number of times.
- In a Fibonacci pair, each division fits just one square, so the numbers shrink as slowly as possible. Gabriel Lamé proved this in 1844: Fibonacci pairs are the worst case. Even so, the number of steps grows only with the number of digits, never with the size of the numbers.
- 1. A prime's only divisors are 1 and itself, and two different primes can't divide each other.
Why it's clever
Listing every divisor of a 300-digit number would take longer than the age of the universe. Euclid's algorithm finds the GCD of two 300-digit numbers in at most about 1,500 short steps, because each remainder is smaller than half the number two steps before.
RSA, the lock-and-key math behind secure websites, uses it every time it makes a new key pair: the public key must be coprime to a number built from the two secret primes. An extended version of the same algorithm then builds the private key.
From the archive
The method is in Book VII of Euclid's Elements, written in Alexandria around 300 BC. Euclid probably collected it from earlier mathematicians rather than inventing it. It is often called the oldest algorithm still in everyday use. The computer scientist Donald Knuth called it the "granddaddy" of all algorithms.