‹ Class 9 · Ch 11
The World of Algorithms · Principle 23 of 29

Subtracting can be slow

Subtraction algorithm can be slow

Stuck? Ask Guru

NCERT: 11.3.3 Euclid's Subtraction Algorithm — Reducing a problem to a simpler one

Think

Try a small pair

Euclid's algorithm found gcd(375, 825) in 7 reductions. Are we close to our goal, an algorithm whose effort grows with the number of digits?
The book tries gcd(99, 2).

About how many reductions will gcd(99, 2) need?

What this lesson covers

The idea

Euclid's subtraction algorithm can take steps proportional to the value of the numbers rather than their number of digits.

Try a small pair

Euclid's algorithm found gcd(375, 825) in 7 reductions. Are we close to our goal, an algorithm whose effort grows with the number of digits? The book tries gcd(99, 2).

About how many reductions will gcd(99, 2) need?

  • About 50
  • About 2
  • About 7

Count the reductions

Run Euclid's subtraction algorithm on gcd(99, 2). Keep an eye on the reduction counter. (Use Run to the end once you see the pattern.)

Steps grow with the value

Euclid's subtraction algorithm can take a number of steps proportional to the value of the numbers, rather than their number of digits.

gcd(99, 2) → gcd(97, 2) → gcd(95, 2) → … → gcd(3, 2) → gcd(2, 1) → gcd(1, 1) → gcd(1, 0): the answer is 1. In general, gcd(2k + 1, 2) needs about k reductions: here 2k + 1 = 99, so k = 49 (the toy counts 51, about 49). A 2-digit number, yet about 50 reductions.

Notes

Euclid's subtraction algorithm can be slow: gcd(2k + 1, 2) needs about k reductions, so its steps grow with the value, not the digits.

Check yourself

Why is gcd(99, 2) so slow with subtraction?

Using the book's rule, about how many reductions does gcd(2k + 1, 2) need when 2k + 1 = 999?

Answer: 499

k = (999 − 1)/2 = 499: about 499 reductions for a 3-digit number.

  • Each step only subtracts 2, so 99 shrinks by 2 at a time — correct. Yes. About 49 subtractions of 2.
  • Because 99 is odd. Odd or even, subtracting 2 each time is slow.
  • Because the gcd is 1. A gcd of 1 is not what makes it slow.