Euler Hall — free maths guides

Euclid's algorithm: replace the pair by the remainder until it stops

A plain-language explanation of the euclidean algorithm. The idea in plain words, a worked example, and a check with the answers shown — part of the free Quantitative Reasoning guides.

gcd(a,b) = gcd(b, a mod b)

Euclid's algorithm finds a gcd by repeated division, without factoring either number:

gcd⁡(a,b)=gcd⁡(b, a mod b),\gcd(a, b) = \gcd(b,\ a \bmod b),

repeated until the remainder is 0. The last non-zero remainder is the gcd.

A worked run ongcd⁡(48,18)\gcd(48, 18):

| step | pair | division | |---|---|---| | 1 | (48, 18) |48=2⋅18+1248 = 2 \cdot 18 + 12 | | 2 | (18, 12) |18=1⋅12+618 = 1 \cdot 12 + 6 | | 3 | (12, 6) |12=2⋅6+012 = 2 \cdot 6 + 0 |

The last non-zero remainder is 6, sogcd⁡(48,18)=6\gcd(48,18) = 6.

Why it works: any common divisor ofaa andbb dividesa−qb=ra - qb = r, so the set of common divisors does not change when the pair is replaced. The numbers shrink, so the process ends — and when it does, the gcd is the last non-zero value.

This is much faster than factoring for large numbers, which is why it is the algorithm a computer uses.

Why the common divisors do not change

The key fact is thatgcd⁡(a,b)=gcd⁡(b,r)\gcd(a, b) = \gcd(b, r) wherer=a mod br = a \bmod b.

To see it, writea=qb+ra = qb + r. Anydd dividing bothaa andbb dividesqbqb, hence dividesa−qb=ra - qb = r. And anydd dividing bothbb andrr dividesqb+r=aqb + r = a. So the two pairs have exactly the same common divisors — and therefore the same greatest one.

That invariance is the whole algorithm: each step preserves the answer while making the numbers smaller, so the answer is unchanged at the end, when one number is 0 and the other is visibly the gcd.

Termination: the remainder is strictly less than the divisor and non-negative, so the pair's second component decreases at every step and must reach 0.

Worked example: a longer Euclid run

Findgcd⁡(1071,462)\gcd(1071, 462).

Step one.1071=2⋅462+1471071 = 2 \cdot 462 + 147. (Check:2⋅462=9242 \cdot 462 = 924, and1071−924=1471071 - 924 = 147.)

Step two.462=3⋅147+21462 = 3 \cdot 147 + 21. 3⋅147=4413 \cdot 147 = 441, remainder 21.)

Step three.147=7⋅21+0147 = 7 \cdot 21 + 0. 7⋅21=1477 \cdot 21 = 147.)

Answer. The last non-zero remainder is 21, sogcd⁡(1071,462)=21\gcd(1071, 462) = 21.

The check.1071÷21=511071 \div 21 = 51 and462÷21=22462 \div 21 = 22: both exact. And 22 = 2 · 11 and 51 = 3 · 17 share no prime, so 21 is the greatest — the check is the coprimality of the two quotients, which is exactly what "greatest" requires.

By the end you can
  • Run Euclid's algorithm to find a gcd
  • Explain why replacing the pair does not change the common divisors
  • Say why the algorithm terminates
Check yourself
  1. 1. Run Euclid ongcd⁡(30,12)\gcd(30, 12).
  2. 2. Run Euclid ongcd⁡(25,15)\gcd(25, 15).
  3. 3. Why can the algorithm not run forever?
Answers

1.30=2⋅12+630 = 2 \cdot 12 + 6;12=2⋅6+012 = 2 \cdot 6 + 0. Answer 6. 2.25=1⋅15+1025 = 1 \cdot 15 + 10;15=1⋅10+515 = 1 \cdot 10 + 5;10=2⋅5+010 = 2 \cdot 5 + 0. Answer 5. 3. Each remainder is smaller than the divisor before it, so the second component strictly decreases; a decreasing sequence of non-negative integers must reach 0.

Work out where to start — free, no accountAnswer six real questions — free, no account