GCD and the Euclidean Algorithm
What does it mean for two numbers to share a divisor — and how do you find the largest one without factoring? The Euclidean Algorithm, proved from scratch.
What is the largest integer that divides both 252 and 105?
You could list all divisors of each and find the intersection. That works here. It doesn't work when the numbers are large. You need a better question: is there a structure to shared divisors that lets you compute without listing?
There is. It's 2300 years old.
Divisibility, precisely
For integers and , we say divides — written — if there exists an integer such that .
Three facts do most of the work in this topic:
- If and , then .
- More generally: if and , then for any integers .
- This last one — divisibility is preserved under linear combinations — is the engine behind everything below.
GCD — what it is
The greatest common divisor is the largest positive integer that divides both and .
Before computing anything, notice what this definition guarantees:
- , because every integer divides 0.
- .
- — order doesn't matter.
- — sign doesn't matter.
The last special case worth naming: when , we call and coprime. They share no common factor larger than 1.
The key observation
Here is the fact that makes efficient computation possible:
Why? The Division Algorithm says: for any integers and , there exist unique integers and with such that .
Now let be any common divisor of and . Then and , so . So divides both and .
Run it backwards: any common divisor of and divides , so it divides both and .
The set of common divisors of and are identical. Their maxima — the GCDs — must be equal.
The Euclidean Algorithm
Apply the observation repeatedly. At each step, replace the larger argument with the remainder. The remainders strictly decrease toward 0. When you hit 0, the last nonzero remainder is the GCD.
Example. :
Answer: .
Example. :
Answer: .
How fast does it terminate?
Each step replaces with where . The second argument strictly decreases. But how many steps?
The worst case is consecutive Fibonacci numbers. Watch what happens with :
Each step peels off exactly one Fibonacci number. After steps you reach .
Since grows like , the number of steps is at most — logarithmic in the input. Even for billion-digit numbers, the algorithm terminates in thousands of steps.
LCM — the companion
The least common multiple is the smallest positive integer divisible by both and .
The two quantities are linked:
Proof. Let . Write , where .
Claim: .
is divisible by and by . ✓
Now suppose is any common multiple of and . Write for some integer . Then means . Since , we get , so .
Therefore and .
Common mistake. This relation holds for two numbers only. in general. Check with .
Worked problems
Problem 1. If and , find all pairs with .
Write , with . Then , so .
Coprime pairs with and :
| 1 | 150 | 1 ✓ |
| 2 | 75 | 1 ✓ |
| 3 | 50 | 1 ✓ |
| 6 | 25 | 1 ✓ |
| 10 | 15 | 5 ✗ |
Pairs : , , , .
Problem 2. Find for any positive integer .
Any common divisor satisfies and , so . Thus .
Consecutive integers are always coprime.
Problem 3. Show that .
. The shift by is invisible.
What this page doesn't answer
The Euclidean Algorithm finds . It doesn't tell you what is beyond "the largest common divisor." There's a deeper characterization — the GCD is the smallest positive integer expressible as a combination .
That's Bezout's Identity, and it's where the real power lives.