Euclidean Algorithm
The Euclidean algorithm finds the greatest common divisor of two integers by repeated division. Divide the larger number by the smaller, then divide the old divisor by the remainder, and keep going until the remainder is 0. The last nonzero remainder is gcd(a, b). Working backward writes the gcd as ax + by.
Before this lesson: Divisibility and the Division Algorithm and, from Pre-Algebra, the Greatest Common Factor
Why repeated division finds the gcd
The greatest common divisor gcd(a, b) is the largest integer that divides both a and b. Pre-Algebra finds it with prime factorizations, which is slow for large numbers. The Euclidean algorithm never factors anything. It rests on one fact.
a = bq + r \implies \gcd(a, b) = \gcd(b, r)
The proof shows that a and b have exactly the same common divisors as b and r, so their greatest common divisors match.
d \mid a \text{ and } d \mid bLet d be any common divisor of a and b.
r = a - bq
Solve the division a = bq + r for r.
d \mid r
r is a combination of a and b, so d divides it (the linear-combination property of divisibility).
e \mid b \text{ and } e \mid rNow let e be any common divisor of b and r.
e \mid bq + r = a
a is a combination of b and r, so e divides a.
\gcd(a, b) = \gcd(b, r)
The two pairs share the same common divisors, so they share the greatest one.
Each step replaces the pair (a, b) with the smaller pair (b, r). The remainders shrink and stay at least 0, so the process must reach a remainder of 0, and gcd(g, 0) = g.
The Euclidean algorithm, step by step
- Divide a by b: write a = bq1 + r1 with 0 ≤ r1 < b.
- If r1 = 0, then gcd(a, b) = b. Stop.
- Otherwise, divide b by r1, then r1 by r2, and so on. Each new division uses the last divisor and the last remainder.
- When a remainder is 0, the remainder just before it is the gcd.
The algorithm is fast. Lamé’s theorem says the number of divisions is at most 5 times the number of decimal digits of the smaller number, so two 100-digit numbers need at most 500 divisions.
Bézout’s identity and the extended Euclidean algorithm
\gcd(a, b) = ax + by \quad \text{for some integers } x \text{ and } yThis is Bézout’s identity, and the integers x and y are called Bézout coefficients. The extended Euclidean algorithm finds them: solve each division for its remainder, then substitute backward from the gcd until only a and b are left. It also shows that ax + by = c has integer solutions exactly when gcd(a, b) divides c.
Euclidean algorithm examples
Example 1: gcd(252, 198)
Use the Euclidean algorithm to find gcd(252, 198).
252 = 198 \cdot 1 + 54
Divide 252 by 198: quotient 1, remainder 54.
198 = 54 \cdot 3 + 36
Divide the old divisor 198 by the remainder 54.
54 = 36 \cdot 1 + 18
Divide 54 by 36.
36 = 18 \cdot 2 + 0
Divide 36 by 18. The remainder is 0, so stop.
\gcd(252, 198) = 18
The last nonzero remainder is the gcd.
Answer: gcd(252, 198) = 18. Check with prime factorization: 252 = 2² · 3² · 7 and 198 = 2 · 3² · 11 share 2 · 3² = 18.
Example 2: gcd(2023, 391)
Find gcd(2023, 391).
2023 = 391 \cdot 5 + 68
Divide 2023 by 391: 391 · 5 = 1955, and 2023 − 1955 = 68.
391 = 68 \cdot 5 + 51
Divide 391 by 68: 68 · 5 = 340, and 391 − 340 = 51.
68 = 51 \cdot 1 + 17
Divide 68 by 51.
51 = 17 \cdot 3 + 0
Divide 51 by 17. The remainder is 0, so stop.
\gcd(2023, 391) = 17
The last nonzero remainder is the gcd.
Answer: gcd(2023, 391) = 17. Indeed 2023 = 17 · 119 and 391 = 17 · 23, and 119 = 7 · 17 shares no factor with 23.
Example 3: Writing 18 as 252x + 198y
Use the divisions from Example 1 to find integers x and y with 252x + 198y = 18.
18 = 54 - 36
Solve the third division, 54 = 36 · 1 + 18, for the remainder 18.
36 = 198 - 54 \cdot 3
Solve the second division for 36.
18 = 54 - (198 - 54 \cdot 3)
Substitute for 36.
18 = 54 - 198 + 3 \cdot 54
Distribute the minus sign (see distributive property).
18 = 4 \cdot 54 - 198
Combine the 54 terms: 1 + 3 = 4 (see combining like terms).
54 = 252 - 198
Solve the first division for 54.
18 = 4(252 - 198) - 198
Substitute for 54.
18 = 4 \cdot 252 - 4 \cdot 198 - 198
Distribute the 4.
18 = 4 \cdot 252 - 5 \cdot 198
Combine the 198 terms.
1008 - 990 = 18
Check: 4 · 252 = 1008 and 5 · 198 = 990.
Answer: x = 4 and y = −5. Keep 54, 36 and 198 as numbers during the substitution; multiplying them out early hides the pattern.
Example 4: A pair with gcd 1
Show that gcd(77, 30) = 1, then find integers x and y with 77x + 30y = 1.
77 = 30 \cdot 2 + 17
Divide 77 by 30.
30 = 17 \cdot 1 + 13
Divide 30 by 17.
17 = 13 \cdot 1 + 4
Divide 17 by 13.
13 = 4 \cdot 3 + 1
Divide 13 by 4.
4 = 1 \cdot 4 + 0
Divide 4 by 1. The last nonzero remainder is 1, so gcd(77, 30) = 1.
1 = 13 - 4 \cdot 3
Solve the fourth division for 1.
1 = 13 - (17 - 13) \cdot 3
Substitute 4 = 17 − 13.
1 = 13 - 3 \cdot 17 + 3 \cdot 13
Distribute the −3.
1 = 4 \cdot 13 - 3 \cdot 17
Combine the 13 terms.
1 = 4(30 - 17) - 3 \cdot 17
Substitute 13 = 30 − 17.
1 = 4 \cdot 30 - 4 \cdot 17 - 3 \cdot 17
Distribute the 4.
1 = 4 \cdot 30 - 7 \cdot 17
Combine the 17 terms.
1 = 4 \cdot 30 - 7(77 - 30 \cdot 2)
Substitute 17 = 77 − 30 · 2.
1 = 4 \cdot 30 - 7 \cdot 77 + 14 \cdot 30
Distribute the −7.
1 = 18 \cdot 30 - 7 \cdot 77
Combine the 30 terms.
540 - 539 = 1
Check: 18 · 30 = 540 and 7 · 77 = 539.
Answer: gcd(77, 30) = 1, and x = −7, y = 18 give 77(−7) + 30(18) = 1. Numbers with gcd 1 are called relatively prime. Since 30 · 18 is 1 more than a multiple of 77, the number 18 acts like 1/30 in arithmetic with remainders mod 77.
Example 5: The largest square tile (word problem)
A room is 444 cm long and 168 cm wide. It will be covered with identical square tiles, with no cutting and no gaps. What is the largest tile side length that works, and how many tiles are needed?
444 = 168 \cdot 2 + 108
The side must divide both 444 and 168, so find their gcd. Divide 444 by 168.
168 = 108 \cdot 1 + 60
Divide 168 by 108.
108 = 60 \cdot 1 + 48
Divide 108 by 60.
60 = 48 \cdot 1 + 12
Divide 60 by 48.
48 = 12 \cdot 4 + 0
Divide 48 by 12. The remainder is 0, so gcd(444, 168) = 12.
444 \div 12 = 37
Count the tiles along the length.
168 \div 12 = 14
Count the tiles along the width.
37 \cdot 14 = 518
Multiply to count all the tiles.
Answer: the largest tile is 12 cm by 12 cm, and the room needs 518 tiles.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Reporting the final remainder 0 as the gcd | “36 = 18 · 2 + 0, so the gcd is 0.” | The gcd is the last nonzero remainder, 18. |
| Dividing the wrong pair next | After 252 = 198 · 1 + 54, dividing 252 by 54 | Each new division uses the last divisor and the last remainder: 198 by 54. |
| Multiplying out during back substitution | Turning 4(252 − 198) − 198 into 216 | Keep a and b as symbols or unmultiplied numbers until the end. |
| Sign errors when distributing | 13 − (17 − 13) · 3 = 13 − 3 · 17 − 3 · 13 | −(17 − 13) · 3 = −3 · 17 + 3 · 13. |
| Thinking the Bézout coefficients are unique | “The only answer is x = 4, y = −5.” | Also 252(−7) + 198(9) = 18. Add 198/18 = 11 to x and subtract 252/18 = 14 from y for another pair. |
| Expecting ax + by = c to be solvable for every c | Solving 12x + 18y = 5 | A solution exists only when gcd(a, b) divides c, and gcd(12, 18) = 6 does not divide 5. |
Related ideas
Once you know the gcd, the least common multiple follows from lcm(a, b) = ab / gcd(a, b). For 252 and 198, that is 49,896 / 18 = 2,772.
Example 4 is the key step of the next lessons. In Modular Arithmetic and Linear Congruences, the extended Euclidean algorithm finds modular inverses, and RSA encryption uses it to build its private key.
Practice problems
- Find gcd(84, 36).
- Find gcd(391, 299).
- Find gcd(1001, 275).
- Find gcd(144, 89). How many divisions does it take?
- Write gcd(84, 36) as 84x + 36y.
- Write gcd(391, 299) as 391x + 299y.
- Find integers x and y with 26x + 11y = 1.
- Use the gcd from Example 1 to find lcm(252, 198).
- Does 12x + 18y = 5 have a solution in integers? Explain.
- Two ropes are 286 cm and 208 cm long. Both are cut into pieces of equal length, as long as possible, with nothing left over. How long is each piece, and how many pieces are there?
Show answers
1.
84 = 36 \cdot 2 + 12
Divide 84 by 36.
36 = 12 \cdot 3 + 0
Divide 36 by 12. The remainder is 0.
Answer: gcd(84, 36) = 12
2.
391 = 299 \cdot 1 + 92
Divide 391 by 299.
299 = 92 \cdot 3 + 23
Divide 299 by 92: 92 · 3 = 276, and 299 − 276 = 23.
92 = 23 \cdot 4 + 0
Divide 92 by 23. The remainder is 0.
Answer: gcd(391, 299) = 23
3.
1001 = 275 \cdot 3 + 176
Divide 1001 by 275: 275 · 3 = 825, and 1001 − 825 = 176.
275 = 176 \cdot 1 + 99
Divide 275 by 176.
176 = 99 \cdot 1 + 77
Divide 176 by 99.
99 = 77 \cdot 1 + 22
Divide 99 by 77.
77 = 22 \cdot 3 + 11
Divide 77 by 22.
22 = 11 \cdot 2 + 0
Divide 22 by 11. The remainder is 0.
Answer: gcd(1001, 275) = 11
4.
144 = 89 \cdot 1 + 55
Divide 144 by 89.
89 = 55 \cdot 1 + 34
Divide 89 by 55.
55 = 34 \cdot 1 + 21
Divide 55 by 34.
34 = 21 \cdot 1 + 13
Divide 34 by 21.
21 = 13 \cdot 1 + 8
Divide 21 by 13.
13 = 8 \cdot 1 + 5
Divide 13 by 8.
8 = 5 \cdot 1 + 3
Divide 8 by 5.
5 = 3 \cdot 1 + 2
Divide 5 by 3.
3 = 2 \cdot 1 + 1
Divide 3 by 2.
2 = 1 \cdot 2 + 0
Divide 2 by 1. The remainder is 0.
Answer: gcd(144, 89) = 1 after 10 divisions. Consecutive Fibonacci numbers make every quotient 1, the slowest possible case, and 10 is exactly Lamé’s bound of 5 times the 2 digits of 89
5.
12 = 84 - 36 \cdot 2
Solve the first division for the remainder 12.
Answer: 12 = 84(1) + 36(−2), so x = 1 and y = −2
6.
23 = 299 - 92 \cdot 3
Solve the second division for 23.
92 = 391 - 299
Solve the first division for 92.
23 = 299 - (391 - 299) \cdot 3
Substitute for 92.
23 = 299 - 3 \cdot 391 + 3 \cdot 299
Distribute the −3.
23 = 4 \cdot 299 - 3 \cdot 391
Combine the 299 terms.
1196 - 1173 = 23
Check: 4 · 299 = 1196 and 3 · 391 = 1173.
Answer: x = −3 and y = 4
7.
26 = 11 \cdot 2 + 4
Divide 26 by 11.
11 = 4 \cdot 2 + 3
Divide 11 by 4.
4 = 3 \cdot 1 + 1
Divide 4 by 3.
3 = 1 \cdot 3 + 0
Divide 3 by 1, so gcd(26, 11) = 1.
1 = 4 - 3
Solve the third division for 1.
1 = 4 - (11 - 4 \cdot 2)
Substitute 3 = 11 − 4 · 2.
1 = 4 - 11 + 4 \cdot 2
Distribute the minus sign.
1 = 3 \cdot 4 - 11
Combine the 4 terms: 1 + 2 = 3.
1 = 3(26 - 11 \cdot 2) - 11
Substitute 4 = 26 − 11 · 2.
1 = 3 \cdot 26 - 6 \cdot 11 - 11
Distribute the 3.
1 = 3 \cdot 26 - 7 \cdot 11
Combine the 11 terms.
78 - 77 = 1
Check: 3 · 26 = 78 and 7 · 11 = 77.
Answer: x = 3 and y = −7
8.
252 \cdot 198 = 49896
Multiply the two numbers.
\dfrac{49896}{18} = 2772Divide by gcd(252, 198) = 18.
Answer: lcm(252, 198) = 2,772
9.
18 = 12 \cdot 1 + 6
Divide 18 by 12.
12 = 6 \cdot 2 + 0
Divide 12 by 6, so gcd(12, 18) = 6.
6 \mid 12x + 18y
6 divides both terms, so it divides every value of 12x + 18y.
6 \nmid 5
But 5 is not a multiple of 6.
Answer: no. Every value of 12x + 18y is a multiple of 6, and 5 is not
10.
286 = 208 \cdot 1 + 78
The piece length must divide both lengths, so find the gcd. Divide 286 by 208.
208 = 78 \cdot 2 + 52
Divide 208 by 78.
78 = 52 \cdot 1 + 26
Divide 78 by 52.
52 = 26 \cdot 2 + 0
Divide 52 by 26, so gcd(286, 208) = 26.
286 \div 26 = 11
Count the pieces from the first rope.
208 \div 26 = 8
Count the pieces from the second rope.
11 + 8 = 19
Add to get the total.
Answer: each piece is 26 cm long, and there are 19 pieces
Euclidean algorithm FAQ
How do you find the gcd with the Euclidean algorithm?
Divide the larger number by the smaller, then repeatedly divide the previous divisor by the previous remainder. When a remainder of 0 appears, the remainder just before it is the gcd.
Why does the Euclidean algorithm work?
If a = bq + r, then a and b have exactly the same common divisors as b and r, so gcd(a, b) = gcd(b, r). Each step keeps the gcd the same while making the numbers smaller.
What is the extended Euclidean algorithm?
It runs the Euclidean algorithm, then substitutes backward through the divisions to write gcd(a, b) = ax + by. The integers x and y are the Bézout coefficients, which are used to find modular inverses.
What is Bézout’s identity?
For any integers a and b, not both 0, there are integers x and y with ax + by = gcd(a, b). In fact, gcd(a, b) is the smallest positive number that can be written in the form ax + by.
Summary
- The Euclidean algorithm uses gcd(a, b) = gcd(b, r), where r is the remainder when a is divided by b.
- Keep dividing the last divisor by the last remainder. The last nonzero remainder is the gcd.
- The number of divisions is small: at most 5 times the number of digits of the smaller number.
- The extended Euclidean algorithm substitutes backward to write gcd(a, b) = ax + by (Bézout’s identity).
- ax + by = c has integer solutions exactly when gcd(a, b) divides c.
Previous lesson: Divisibility and the Division Algorithm
Next lesson: Modular Arithmetic
Back to the Discrete Math Index