The Euclidean Algorithm for Greatest Common Divisors, Step by Step
Learn to find any greatest common divisor by repeated division, with worked examples, a proof that it works and the reason it is fast.
Key takeaways
- Replace the pair (a, b) with (b, r), where r is the remainder of a divided by b, until the remainder is 0.
- It works because a pair and its replacement have exactly the same common divisors.
- The remainders shrink quickly, so the number of steps grows only in proportion to the number of digits.
- The result reduces fractions, gives least common multiples and, when run backward, solves equations in integers.
The Euclidean algorithm finds the greatest common divisor of two whole numbers by dividing again and again. Divide the larger number by the smaller, keep the remainder, then repeat with the smaller number and that remainder. When the remainder reaches 0, the last nonzero remainder is the answer.
The greatest common divisor of a and b, written gcd(a, b), is the largest positive whole number that divides both. The method needs no factoring, works quickly even for numbers with hundreds of digits, and appears in Book VII of Euclid's Elements, written around 300 BCE.
The method in four steps
Start with two positive whole numbers a and b, with a ≥ b.
- Divide a by b and write a = bq + r, where q is the quotient and r is the remainder with 0 ≤ r < b.
- If r = 0, stop. The answer is b.
- Otherwise replace a with b, and b with r.
- Return to step 1.
If you happen to start with the smaller number first, nothing goes wrong. The first division has quotient 0 and simply swaps the two.
A worked example
Find gcd(1071, 462).
1071 = 2 × 462 + 147
462 = 3 × 147 + 21
147 = 7 × 21 + 0 The last nonzero remainder is 21, so gcd(1071, 462) = 21. You can confirm that 1071 = 21 × 51 and 462 = 21 × 22, and that 51 and 22 share no factor larger than 1.
Check each line by multiplying: 2 × 462 = 924 and 924 + 147 = 1071; 3 × 147 = 441 and 441 + 21 = 462; 7 × 21 = 147. A quick check like this catches most slips.
Why it works
The heart of the method is one observation. Suppose a = bq + r. Then any number d that divides both a and b also divides a − bq, which is r. In the other direction, any d that divides both b and r also divides bq + r, which is a.
So the pairs (a, b) and (b, r) have exactly the same set of common divisors. In particular they have the same largest one:
gcd(a, b) = gcd(b, r) Each step keeps the gcd unchanged while making the numbers smaller. This is a fine example of an invariant. At the final step you reach a pair (g, 0). Every number divides 0, so the common divisors of g and 0 are just the divisors of g, and the largest is g itself.
The process must end because the remainders form a strictly decreasing list of whole numbers that are at least 0. Such a list cannot go on forever.
A picture with squares
Imagine a rectangle 1071 units long and 462 units wide, and cut off the largest squares you can.
- Two squares of side 462 fit, leaving a strip 147 wide.
- Three squares of side 147 fit in what remains, leaving a strip 21 wide.
- Seven squares of side 21 fill that strip exactly.
The side of the last square, 21, measures the whole rectangle: it tiles every larger square and so the original shape. The quotients 2, 3 and 7 are also the terms of a continued fraction, since 1071/462 = 2 + 1/(3 + 1/7). That link is developed in continued fractions.
Euclid himself described the process with repeated subtraction of the smaller length from the larger. Division with remainder is the same thing, with many subtractions bundled into one step.
Why it is fast
Compare the alternative taught in many classrooms, which is to factor both numbers into primes and keep what they share. For small numbers that is fine. For numbers with hundreds of digits, factoring is believed to be very hard, while the Euclidean algorithm finishes almost instantly.
The reason is that remainders shrink fast. In any division a = bq + r with a ≥ b, the remainder satisfies r < a/2. There are two cases.
if b ≤ a/2: r < b ≤ a/2
if b > a/2: q = 1, so r = a − b < a/2 After two steps the number playing the role of a has become r, so it has been cut by more than half. Gabriel Lamé proved a sharper statement in 1844: the number of divisions is never more than five times the number of decimal digits in the smaller input.
The slowest case
Which inputs make the algorithm work hardest? Those where every quotient is as small as possible, namely consecutive Fibonacci numbers. In that sequence each term is the sum of the two before it.
89 = 1 × 55 + 34
55 = 1 × 34 + 21
34 = 1 × 21 + 13
21 = 1 × 13 + 8
13 = 1 × 8 + 5
8 = 1 × 5 + 3
5 = 1 × 3 + 2
3 = 1 × 2 + 1
2 = 2 × 1 + 0 Nine divisions are needed to learn that gcd(89, 55) = 1. Compare the first example, where much larger numbers took only three. The speed depends on the quotients, and large quotients make rapid progress. See Fibonacci divisibility patterns for more about this sequence.
Compared with factoring
Here is the factoring route for the first example, so you can see the difference in effort. First you must discover that 1071 = 3² × 7 × 17, which means testing small primes one after another. Then you find 462 = 2 × 3 × 7 × 11. The primes they share are one 3 and one 7, and 3 × 7 = 21.
Both routes agree, as they must. The factoring route also tells you more, namely the full structure of each number. You pay for that extra information in search time, and if all you want is the common divisor, the payment is wasted.
The gap widens sharply with size. Suppose each input were the product of two unfamiliar primes with a hundred digits apiece. Nobody knows a practical way for an ordinary computer to factor an arbitrary number of that kind quickly. Lamé's bound says the Euclidean algorithm would need at most about a thousand divisions, which is no work at all for a machine. The difficulty of factoring is a subject of its own.
A shortcut with negative remainders
You may allow remainders that are negative and use whichever is smaller in size. Since divisibility ignores sign, you can simply continue with the absolute value.
Consider gcd(89, 55) again. Instead of 89 = 1 × 55 + 34, write 89 = 2 × 55 − 21. Continue with 55 and 21: 55 = 3 × 21 − 8. Then 21 = 3 × 8 − 3, next 8 = 3 × 3 − 1, and finally 3 = 3 × 1 + 0. The answer is 1 after five divisions in place of nine.
The justification is unchanged. If a = bq − r, every common divisor of a and b divides r, and every common divisor of b and r divides a. This version never lets the remainder exceed half the divisor, so it guarantees at least a halving at each step. By hand it is a pleasant saving. In a program, the plain version is usually simpler to write and easier to check.
What you can do with the answer
To reduce a fraction to lowest terms, divide top and bottom by their gcd. Here 462/1071 = 22/51.
To find a least common multiple of two positive numbers, divide their product by their gcd. For 1071 and 462 this is 1071 × 22 = 23,562. The article on least common multiples explains why.
To handle three or more numbers, work in pairs: gcd(a, b, c) = gcd(gcd(a, b), c). For example, to find gcd(1071, 462, 140), use the 21 already found and continue.
140 = 6 × 21 + 14
21 = 1 × 14 + 7
14 = 2 × 7 + 0 so the gcd of all three is 7 To solve equations such as 1071x + 462y = 21 in integers, run the steps in reverse. That is the extended Euclidean algorithm, which also computes the modular inverses used in cryptography.
Writing it as a program
The algorithm is short enough to fit in a few lines of pseudocode.
function gcd(a, b): a and b are whole numbers, 0 or more
while b is not 0:
r = remainder of a divided by b
a = b
b = r
return a By convention gcd(a, 0) = a, and gcd(0, 0) is usually defined as 0, even though there is no largest divisor of 0. For negative inputs, take absolute values first, since divisibility ignores sign.
There is also a variant, often called the binary gcd algorithm, that uses only subtraction, comparison and halving. It takes advantage of how quickly computers divide by 2.
Common mistakes
- Reporting the final remainder, 0, as the answer. The answer is the last nonzero remainder, which is also the last divisor used.
- Carrying the quotient into the next line. Only the divisor and the remainder move forward.
- Accepting a remainder that is as large as the divisor. If r ≥ b, the quotient was too small.
- Thinking a gcd of 1 means the numbers are prime. It means they are coprime, sharing no prime factor. The numbers 8 and 15 are coprime and neither is prime.
Practice
Try gcd(2024, 748) before reading on.
2024 = 2 × 748 + 528
748 = 1 × 528 + 220
528 = 2 × 220 + 88
220 = 2 × 88 + 44
88 = 2 × 44 + 0 The answer is 44. As a check, 2024 = 44 × 46 and 748 = 44 × 17, and 46 and 17 have no common factor.
Now test your understanding with a question that needs no long calculation. What is gcd(n, n + 1) for a positive whole number n? One step of the algorithm replaces the pair with (n, 1), so the answer is always 1. Similarly, gcd(n, n + 2) equals gcd(n, 2), which is 2 when n is even and 1 when n is odd.
Common questions
Does the order of the two numbers matter?
No. gcd(a, b) = gcd(b, a), and the algorithm sorts them out in its first step.
Does the method work for anything besides integers?
Yes. Polynomials with rational or real coefficients can be divided with remainder, where the remainder has lower degree than the divisor, and the same procedure finds a greatest common polynomial factor. Number systems that allow such a division are called Euclidean domains.