The GCD and the Euclidean Algorithm
The gcd of two numbers is the biggest number that divides both. You don't need to factor anything to find it: swapping (a, b) for (b, a mod b) keeps the same common divisors while the numbers shrink fast, and running the steps backward writes the gcd as sa + tb.
What the symbols mean
- Greatest common divisor
The largest integer dividing both a and b.
- Remainder
What's left after dividing a by b.
- Bézout coefficient
An integer multiplier of a (possibly negative).
- Bézout coefficient
An integer multiplier of b.
Explanation
The greatest common divisor of two numbers is simply the biggest number that goes evenly into both: for 12 and 18 it's 6. The name says exactly that, read backward: a divisor they have in common, the greatest one. It's what you use to reduce a fraction to lowest terms, and it turns out to be the key to dividing in modular arithmetic later in this unit. The formula looks cryptic, but it just says: replace the bigger number with the remainder (, what's left after dividing by ) and the answer doesn't change. And says the gcd can be built from and using whole-number multipliers and . Two small ideas; bear with them.
You could find a gcd by factoring both numbers, as in the last lesson, but factoring big numbers is slow. Euclid found something much better about 2,300 years ago.
Why the algorithm works. If , then any common divisor of and also divides , and any common divisor of and divides . So and have exactly the same common divisors, and in particular the same greatest one. Notice what that buys you: the pair got smaller, but the answer didn't change. Keep going, and when the remainder hits 0 the last nonzero remainder is the gcd, since .
so .
Bézout's identity. Each line above writes a remainder in terms of the two numbers before it. So you can run the steps backward, substituting as you go, and write the gcd as a combination of the original two numbers: . This extended Euclidean algorithm looks like a curiosity now, but it's exactly how computers find modular inverses, which you'll need two lessons from now.
Speed. The remainders at least halve every two steps, so the algorithm takes at most about twice as many steps as the number of bits in the numbers. That's fast even for numbers hundreds of digits long.
Once you have the gcd, the least common multiple comes for free: . The product counts the shared factors twice, and dividing by the gcd removes the extra copy.
See it
Worked examples
Find and write it as .
- 1
and . The remainder hit 0, so the last nonzero one, 12, is the gcd.
Practice
Type your answer and check it. Solve them all to complete the lesson.
Find .
Find .
How many lattice points, counting both ends, lie on the segment from to ?
Find the integer with when .
A floor measuring 252 cm by 198 cm is to be covered with identical square tiles, with no cutting. What is the largest tile side length that works, in cm?
Find the integer with such that for some integer .
The hardest one here. Get it, and you've got this lesson.
How many division steps does Euclid's algorithm take to compute ?
Where it's used
Euclid's algorithm, about 2,300 years old, runs inside every RSA implementation to compute private keys, and in every program that reduces fractions.