See How Things Work

The Euclidean algorithm

Unit
Factors, Multiples & Primes
Part
3. GCD & LCM
Lesson
3.3

The Euclidean algorithm finds the greatest common divisor of two numbers with a few divisions and no factorizations: divide, then divide the divisor by the remainder, and repeat until the remainder is 0. This lesson proves why the last nonzero remainder is the gcd, why the process always stops, and how it goes back to Euclid’s Elements.

The key fact

Listing divisors (lesson “The greatest common divisor”) and factoring into primes (lesson “Finding the GCD from prime factorizations”) both become slow for large numbers. A division with remainder replaces a pair of numbers by a smaller pair with the same gcd.

Theorem .

Let a and b be whole numbers with b ≥ 1, and let a = q × b + r be the division of a by b, with 0 ≤ r < b (the division algorithm). Then a and b have exactly the same common divisors as b and r. In particular, gcd(a, b) = gcd(b, r).

Proof.

Let d divide a and b. Then d | q × b by the rule on multiples, and q × b ≤ a, so d divides the difference a − q × b = r by the rule on differences (lesson “Divisibility”). So d is a common divisor of b and r.

Conversely, let d divide b and r. Then d | q × b, and d divides the sum q × b + r = a. So d is a common divisor of a and b.

The two pairs have the same common divisors. Neither pair is 0 and 0, since b ≥ 1, so both have a greatest common divisor, and it is the same number.

This is the lemma on which the Euclidean algorithm is based,⁠[1][2] and the proof is the usual one: the two pairs have the same common divisors, so they have the same greatest common divisor.⁠[3]

Example (One division).

Use one division to replace gcd(210, 154) by a gcd of smaller numbers.

  1. 210 = 1 × 154 + 56, with 0 ≤ 56 < 154.
  2. By the Theorem, gcd(210, 154) = gcd(154, 56).
  3. For example, 2 divides 210 and 154; it also divides 56 = 28 × 2. And 14 divides 154 (= 11 × 14) and 56 (= 4 × 14); it also divides 210 = 15 × 14.

Answer: gcd(210, 154) = gcd(154, 56), a pair whose larger number is smaller than 210.

The algorithm

Apply the Theorem again and again. Each new pair is the old divisor and the old remainder.

  1. Divide a by b: a = q₁ × b + r₁.
  2. If r₁ = 0, stop. Otherwise divide b by r₁: b = q₂ × r₁ + r₂.
  3. If r₂ = 0, stop. Otherwise divide r₁ by r₂, and so on: each time divide the last divisor by the last remainder.
  4. When a remainder is 0, the divisor of that last division, the last nonzero remainder, is gcd(a, b).

This procedure is the Euclidean algorithm.⁠[4] Its result is the last nonzero remainder in the sequence of divisions.⁠[5]

Theorem .

For whole numbers a and b with b ≥ 1, the Euclidean algorithm stops after at most b divisions, and its last nonzero remainder is gcd(a, b).

Proof.

It stops. Each remainder is less than its divisor, and the next divisor is that remainder, so the remainders decrease: b > r₁ > r₂ > … ≥ 0. A decreasing list of whole numbers below b has at most b members, so after at most b divisions a remainder is 0.⁠[6]

Its result is the gcd. Let the last division be rₙ₋₁ = qₙ₊₁ × rₙ + 0, with rₙ the last nonzero remainder (or b itself, if already r₁ = 0). By the Theorem of Section 1, applied to every division, gcd(a, b) = gcd(b, r₁) = gcd(r₁, r₂) = … = gcd(rₙ₋₁, rₙ). The last division shows rₙ | rₙ₋₁, so gcd(rₙ₋₁, rₙ) = rₙ (lesson “The greatest common divisor”, the Proposition on one number dividing the other). This is also gcd(rₙ, 0) = rₙ, the form the chain takes in Rosen.⁠[5]

Example (gcd(210, 154)).

Find gcd(210, 154) with the Euclidean algorithm.

  1. 210 = 1 × 154 + 56.
  2. 154 = 2 × 56 + 42.
  3. 56 = 1 × 42 + 14.
  4. 42 = 3 × 14 + 0. The remainder is 0: stop.

Answer: the last nonzero remainder is 14, so gcd(210, 154) = 14. Check: 210 = 15 × 14 and 154 = 11 × 14, and 15 and 11 have no common divisor except 1.

Example (Four-digit numbers).

Find gcd(2,035, 851).

  1. 2,035 = 2 × 851 + 333 (2 × 851 = 1,702).
  2. 851 = 2 × 333 + 185 (2 × 333 = 666).
  3. 333 = 1 × 185 + 148.
  4. 185 = 1 × 148 + 37.
  5. 148 = 4 × 37 + 0.

Answer: gcd(2,035, 851) = 37. Check: 2,035 = 55 × 37 and 851 = 23 × 37. By factorization the same answer needs 2,035 = 5 × 11 × 37 and 851 = 23 × 37, and finding the factor 23 of 851 means trial divisions by every prime up to 23. Five divisions were enough here.

3021219933330 = 1 × 21 + 921 = 2 × 9 + 39 = 3 × 3 + 0
Figure. The Euclidean algorithm as cutting squares. From the 30 by 21 rectangle, cut as many squares of side 21 as fit; each division x = q × y + r cuts q squares of side y and leaves a rectangle of sides y and r, which is cut in the same way. The colors match the divisions listed below the rectangle. The last squares, of side 3, fill their rectangle exactly, and 3 = gcd(30, 21): a square of side d tiles the whole rectangle exactly when d divides both sides.
Example (Edge cases: the smaller number first, a divisor, and 0).

Find gcd(35, 120), gcd(91, 13) and gcd(17, 0).

  1. gcd(35, 120): the first division is 35 = 0 × 120 + 35, which only swaps the pair. Then 120 = 3 × 35 + 15, 35 = 2 × 15 + 5, 15 = 3 × 5 + 0. The gcd is 5.
  2. gcd(91, 13): 91 = 7 × 13 + 0 at once. The last nonzero remainder is the divisor 13 itself, so the gcd is 13 (13 | 91).
  3. gcd(17, 0): no division is possible, since the divisor would be 0. But gcd(17, 0) = 17 directly, by the lesson “The greatest common divisor”.

Answer: 5, 13 and 17. Check: 35 = 7 × 5, 120 = 24 × 5; 91 = 7 × 13.

Example (A common mistake: dividing the wrong pair, or reporting 0).

Someone computes gcd(144, 60): “144 = 2 × 60 + 24, then 144 = 6 × 24 + 0, so the gcd is 0.” Find the two errors.

  1. The second division must be of the last divisor by the last remainder: 60 by 24, not 144 by 24. 60 = 2 × 24 + 12, then 24 = 2 × 12 + 0.
  2. The answer is the last nonzero remainder, 12, not the remainder 0 that ends the algorithm. A gcd is never 0 when the numbers are not both 0: 1 divides both.

Answer: gcd(144, 60) = 12. Check: 144 = 12 × 12 and 60 = 5 × 12. (Dividing 144 by 24 happened to give remainder 0, because 24 | 144, but 24 does not divide 60: 60 = 2 × 24 + 12.)

Game

Run the Euclidean algorithm

Round
1
Score
0
Streak
0

Find gcd(749, 469) with the Euclidean algorithm. Type the remainder of each division; the divisor and the remainder then make the next division. When a remainder is 0, give the gcd.

  1. 1.749 divided by 469 leaves remainder
Corollary .

Every common divisor of a and b divides gcd(a, b).

Proof.

Let b ≥ 1 (if b = 0, then gcd(a, 0) = a and every common divisor divides a). A common divisor d of a and b is, by the Theorem of Section 1, a common divisor of b and r₁, then of r₁ and r₂, and so on down the divisions. So d divides every remainder, in particular the last nonzero one, which is gcd(a, b).

This is the corollary that Euclid attaches to his Proposition VII.2: “If a number measures two numbers, then it also measures their greatest common measure.”⁠[7] The previous lesson proved it from prime factorizations; this proof needs no factorization at all.

Euclid’s version: subtraction

Euclid’s own procedure subtracts: in Proposition 1 of Book VII, “the less is continually subtracted in turn from the greater”,⁠[8] and Proposition 2 uses the same process “to find the greatest common measure of two given numbers not relatively prime”.⁠[9] Subtracting b from a as long as the result is at least b is a division with remainder done step by step, since division can be seen as repeated subtraction (lesson “Division as sharing and as grouping”); and each subtraction keeps the gcd:

Proposition .

If a ≥ b ≥ 1, then gcd(a, b) = gcd(a − b, b).

Proof.

This is the Theorem of Section 1 with a written as 1 × b + (a − b), except that a − b need not be less than b; the proof there did not use r < b. A common divisor of a and b divides a − b, and a common divisor of a − b and b divides (a − b) + b = a.

Example (Euclid’s subtraction on 30 and 21).

Find gcd(30, 21) by repeated subtraction, and compare with division.

  1. (30, 21) → 30 − 21 = 9: (9, 21) → 21 − 9 = 12: (9, 12) → 12 − 9 = 3: (9, 3).
  2. (9, 3) → 9 − 3 = 6: (6, 3) → 6 − 3 = 3: (3, 3). The two numbers are equal; gcd(3, 3) = 3.
  3. With division: 30 = 1 × 21 + 9, 21 = 2 × 9 + 3, 9 = 3 × 3 + 0. Three divisions instead of five subtractions.

Answer: gcd(30, 21) = 3 either way. Each division does at once all the subtractions of the same number, as the squares in the figure show.

Historical note.

The method is given in Book VII of the Elements, and today it is called the Euclidean algorithm, although there is historical evidence that it is older than Euclid,⁠[10] by at least a century according to Burton’s history of mathematics.⁠[11] It has been known since ancient times and is named after Euclid, who included a description of it in the Elements.⁠[4]

How fast it is

Gabriel Lamé (1795–1870)⁠[12][13] proved that the number of divisions in the Euclidean algorithm is at most five times the number of digits of the smaller number.⁠[12][14] (The count starts with the division of the larger number by the smaller; a first division that only swaps the pair, as in gcd(35, 120) above, is one more.) For two four-digit numbers that is at most 20 divisions, while finding prime factorizations can take far longer: computing the gcd from the factorizations is inefficient, because finding prime factorizations is time-consuming.⁠[15]

Example (Many divisions: 89 and 55).

Find gcd(89, 55) and count the divisions.

  1. 89 = 1 × 55 + 34; 55 = 1 × 34 + 21; 34 = 1 × 21 + 13; 21 = 1 × 13 + 8.
  2. 13 = 1 × 8 + 5; 8 = 1 × 5 + 3; 5 = 1 × 3 + 2; 3 = 1 × 2 + 1; 2 = 2 × 1 + 0.

Answer: gcd(89, 55) = 1, after 9 divisions. Every quotient but the last is 1, so the remainders shrink as slowly as they can. Lamé’s bound allows 5 × 2 = 10 divisions, since 55 has two digits.

Looking ahead.

The gcd of a and b can also be written as a multiple of a plus a multiple of b, but in general one of the multipliers must be a negative number, as in gcd(6, 14) = 2 = (−2) × 6 + 1 × 14.⁠[16] The equation that writes gcd(a, b) in this way is called Bézout’s identity;⁠[17] it is taught in the unit Divisibility Theory, after the negative numbers. The least common multiple of two numbers, which can be found from their gcd, is the subject of the lessons “The least common multiple” and “Why the GCD times the LCM equals the product”.

Exercises

Exercise .

Find gcd(132, 84) with the Euclidean algorithm.

▸ Show solution

132 = 1 × 84 + 48; 84 = 1 × 48 + 36; 48 = 1 × 36 + 12; 36 = 3 × 12 + 0. So gcd(132, 84) = 12. Check: 132 = 11 × 12, 84 = 7 × 12.

Exercise .

Find gcd(1,276, 551).

▸ Show solution

1,276 = 2 × 551 + 174; 551 = 3 × 174 + 29; 174 = 6 × 29 + 0. So gcd(1,276, 551) = 29. Check: 1,276 = 44 × 29 and 551 = 19 × 29.

Exercise .

Find gcd(56, 21) by Euclid’s repeated subtraction, then by division.

▸ Show solution

56 − 21 = 35: (35, 21); 35 − 21 = 14: (14, 21); 21 − 14 = 7: (14, 7); 14 − 7 = 7: (7, 7). So gcd(56, 21) = 7. With division: 56 = 2 × 21 + 14, 21 = 1 × 14 + 7, 14 = 2 × 7 + 0.

Exercise .

Find gcd(377, 233), count the divisions, and compare with Lamé’s bound.

▸ Show solution

377 = 1 × 233 + 144; 233 = 1 × 144 + 89; 144 = 1 × 89 + 55; 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.

gcd(377, 233) = 1 after 12 divisions. Lamé’s bound for the three-digit number 233 is 5 × 3 = 15, and 12 ≤ 15.

Exercise .

Prove that in every division x = q × y + r with 1 ≤ y ≤ x, the remainder satisfies 2 × r < x. What does this say about the remainders of the Euclidean algorithm?

▸ Show solution

Let x = q × y + r with 1 ≤ y ≤ x and 0 ≤ r < y. Then q ≥ 1 (since y ≤ x, the multiple 1 × y does not exceed x), so x = q × y + r ≥ y + r. As r < y, r + r < y + r ≤ x. So 2 × r < x.

In the algorithm the remainder two steps later, rₖ₊₂, comes from dividing rₖ by rₖ₊₁, so 2 × rₖ₊₂ < rₖ: every two divisions at least halve the remainder. Example: 210, 154, 56, 42, 14: 2 × 56 < 210 and 2 × 14 < 56.

Exercise .

Use one step of the algorithm to show that gcd(n + 1, n) = 1 and gcd(2 × n + 1, n) = 1 for every whole number n ≥ 1.

▸ Show solution

n + 1 = 1 × n + 1, so by the Theorem of Section 1 (whose proof does not use r < b, so n = 1 is allowed too) gcd(n + 1, n) = gcd(n, 1) = 1: consecutive numbers are relatively prime. In the same way 2 × n + 1 = 2 × n + 1 gives gcd(2 × n + 1, n) = gcd(n, 1) = 1.

Example: gcd(41, 20) = 1, since 41 = 2 × 20 + 1.

Summary

  1. If a = q × b + r with 0 ≤ r < b, then a, b and b, r have the same common divisors, so gcd(a, b) = gcd(b, r).
  2. The Euclidean algorithm divides, then divides the divisor by the remainder, until a remainder is 0; the last nonzero remainder is gcd(a, b).
  3. It stops because the remainders decrease; Lamé proved it needs at most five divisions per digit of the smaller number.
  4. Every common divisor divides every remainder, hence divides the gcd.
  5. Euclid’s own version subtracts the smaller number from the larger; a division does many such subtractions at once.

Sources

  1. 1.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.3, “Primes and Greatest Common Divisors” (4.3.7, “The Euclidean Algorithm”), Lemma 1, p. 283 (the whole topic: pp. 282–285).
    “Let a = bq + r, where a, b, q, and r are integers. Then gcd(a, b) = gcd(b, r).”
    Used for: if a = q × b + r, then gcd(a, b) = gcd(b, r): the lemma on which the Euclidean algorithm is based.
  2. 2.
    David M. Burton, Elementary Number Theory (7th ed., McGraw-Hill, 2011).
    Chapter 2, “Divisibility Theory in the Integers”, section 2.4, “The Euclidean Algorithm”, p. 26 (the whole topic: pp. 26–27).
    “We argue that rn, the last nonzero remainder that appears in this manner, is equal to gcd(a, b). Our proof is based on the lemma below.”
    Used for: the proof of the algorithm rests on the lemma gcd(a, b) = gcd(b, r).
  3. 3.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.3, “Primes and Greatest Common Divisors” (4.3.7, “The Euclidean Algorithm”), proof of Lemma 1, p. 283 (the whole topic: pp. 282–285).
    “If we can show that the common divisors of a and b are the same as the common divisors of b and r, we will have shown that gcd(a, b) = gcd(b, r), because both pairs must have the same greatest common divisor.”
    Used for: the proof: the two pairs have the same common divisors, so the same greatest common divisor.
  4. 4.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.3, “Primes and Greatest Common Divisors” (4.3.7, “The Euclidean Algorithm”), pp. 282–283 (the whole topic: pp. 282–284).
    “This algorithm has been known since ancient times. It is named after the ancient Greek mathematician Euclid, who included a description of this algorithm in his book The Elements.”
    Used for: the name Euclidean algorithm; the method has been known since ancient times and is named after Euclid, who described it in the Elements.
  5. 5.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.3, “Primes and Greatest Common Divisors” (4.3.7, “The Euclidean Algorithm”), p. 284 (the whole topic: pp. 283–284).
    “Hence, the greatest common divisor is the last nonzero remainder in the sequence of divisions.”
    Used for: the result of the algorithm is the last nonzero remainder; the chain ends with gcd(rn, 0) = rn.
  6. 6.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.3, “Primes and Greatest Common Divisors” (4.3.7, “The Euclidean Algorithm”), p. 284 (the whole topic: pp. 283–284).
    “Eventually a remainder of zero occurs in this sequence of successive divisions, because the sequence of remainders a = r0 > r1 > r2 > ⋯ ≥ 0 cannot contain more than a terms.”
    Used for: the algorithm stops because the remainders form a decreasing list of whole numbers.
  7. 7.
    Euclid, trans. T. L. Heath, ed. D. E. Joyce, “Euclid’s Elements, Book VII”, Clark University.
    “If a number measures two numbers, then it also measures their greatest common measure.”
    Used for: Euclid’s corollary to Elements VII.2: every common divisor divides the gcd.
  8. 8.
    Euclid, trans. T. L. Heath, ed. D. E. Joyce, “Euclid’s Elements, Book VII”, Clark University.
    “When two unequal numbers are set out, and the less is continually subtracted in turn from the greater, if the number which is left never measures the one before it until a unit is left, then the original numbers are relatively prime.”
    Used for: Euclid, Elements VII.1: the less number is continually subtracted from the greater.
  9. 9.
    Euclid, trans. T. L. Heath, ed. D. E. Joyce, “Euclid’s Elements, Book VII”, Clark University.
    “To find the greatest common measure of two given numbers not relatively prime.”
    Used for: Euclid, Elements VII.2: the same process finds the greatest common measure (the gcd).
  10. 10.
    David M. Burton, Elementary Number Theory (7th ed., McGraw-Hill, 2011).
    Chapter 2, “Divisibility Theory in the Integers”, section 2.4, “The Euclidean Algorithm”, p. 26 (the whole topic: pp. 26–27).
    “A more efficient process, involving repeated application of the Division Algorithm, is given in the seventh book of the Elements. Although there is historical evidence that this method predates Euclid, today it is referred to as the Euclidean Algorithm.”
    Used for: the method is in Book VII of the Elements, is called the Euclidean algorithm, and there is historical evidence that it is older than Euclid.
  11. 11.
    David M. Burton, The History of Mathematics: An Introduction (7th ed., McGraw-Hill, 2011).
    Chapter 4, “The Alexandrian School: Euclid”, section 4.3, “Euclid’s Number Theory” (“The Algorithm of Euclid”), p. 173 (the whole topic: pp. 173–175).
    “Although there is historical evidence that this method predates Euclid by at least a century, it today goes under the name “Euclidean algorithm.””
    Used for: according to Burton’s history, the method predates Euclid by at least a century.
  12. 12.
    David M. Burton, Elementary Number Theory (7th ed., McGraw-Hill, 2011).
    Chapter 2, “Divisibility Theory in the Integers”, section 2.4, “The Euclidean Algorithm”, p. 28 (the whole topic: pp. 27–28).
    “The French mathematician Gabriel Lamé (1795–1870) proved that the number of steps required in the Euclidean Algorithm is at most five times the number of digits in the smaller integer.”
    Used for: Lamé (1795–1870) proved that the number of divisions is at most five times the number of digits of the smaller number.
  13. 13.
    J. J. O’Connor and E. F. Robertson, “Gabriel Lamé”, MacTutor History of Mathematics Archive, University of St Andrews.
    “Born 22 July 1795 Tours, France Died 1 May 1870 Paris, France”
    Used for: Lamé’s dates, 1795–1870 (a second source).
  14. 14.
    J. J. O’Connor and E. F. Robertson, “Gabriel Lamé”, MacTutor History of Mathematics Archive, University of St Andrews.
    “he showed that the number of divisions in the Euclidean algorithm never exceeds five times the number of digits in the smaller number.”
    Used for: Lamé’s bound on the number of divisions (a second source).
  15. 15.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.3, “Primes and Greatest Common Divisors” (4.3.7, “The Euclidean Algorithm”), p. 282 (the whole topic: pp. 282–284).
    “Computing the greatest common divisor of two integers directly from the prime factorizations of these integers is inefficient. The reason is that it is time-consuming to find prime factorizations.”
    Used for: computing the gcd from prime factorizations is inefficient, because finding prime factorizations is time-consuming.
  16. 16.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.3, “Primes and Greatest Common Divisors” (4.3.8, “gcds as Linear Combinations”), p. 285 (the whole topic: pp. 285–287).
    “For example, gcd(6, 14) = 2, and 2 = (−2) ⋅ 6 + 1 ⋅ 14.”
    Used for: writing the gcd as a combination of a and b needs negative numbers in general: gcd(6, 14) = 2 = (−2) × 6 + 1 × 14.
  17. 17.
    Kenneth H. Rosen, Discrete Mathematics and Its Applications (8th ed., McGraw-Hill, 2019).
    Chapter 4, “Number Theory and Cryptography”, section 4.3, “Primes and Greatest Common Divisors” (4.3.8, “gcds as Linear Combinations”), Definition 6, p. 285 (the whole topic: pp. 285–287).
    “Also, the equation gcd(a, b) = sa + tb is called Bézout’s identity.”
    Used for: the name Bézout’s identity for the equation gcd(a, b) = s × a + t × b.