T TestsWorld
Lesson 6 of 8 Euclid and Āryabhaṭa ⏱ 5 min

Euclid's subtraction algorithm

🤔
Think first
How can subtracting help you find a greatest common divisor?
🎯 In this lesson you'll learn
  • ✓Use gcd(m, n) = gcd(n, m − n)
  • ✓See why subtraction can be slow

Key fact (m ≥ n): gcd(m, n) = gcd(n, m − n).

✏️

Why: if d divides m = ad and n = bd, then m − n = (a − b)d, so d divides m − n. Conversely, if d divides n and m − n, it divides their sum m. So both pairs have the same common divisors. In pictures: if blocks of length d tile m and n, they also tile m − n.

Euclid's algorithm for gcd(m, n):

  1. If m < n, swap them.
  2. If n = 0, the answer is m.
  3. Otherwise, reduce to gcd(n, m − n).

gcd(375, 825): swap → (825, 375) → (375, 450) → swap (450, 375) → (375, 75) → (75, 300) → (75, 225) → (75, 150) → (75, 75) → (75, 0) → 75. That is 7 reductions.

The catch: gcd(99, 2) goes (97, 2), (95, 2), …, (3, 2), (2, 1), (1, 1), (1, 0): about 49 reductions. For gcd(2k + 1, 2) it takes about k steps, so the work grows with the size of the number, not its number of digits.

Euclid wrote this down in his book Elements; historians think earlier Greek mathematicians knew it.

ddddddddddddm = 12 blocksddddddddddddn = 5 blocksm − n = 7 blocksso gcd(m, n) = gcd(n, m − n)

Tap the diagram to enlarge it

If d fits exactly into m and n, it fits exactly into m − n too.
✨
Did you know?
Euclid recorded this method in his book Elements over 2000 years ago.
📌 Remember these
  1. Swap if m < n
  2. Stop at gcd(m, 0) = m
  3. gcd(99, 2) is slow by subtraction
✅ Quick check
← →