Learnify Academy is a tuition centre in Bahrain. Classes are for students in Bahrain only.Tuition classes in Bahrain only

Euclid’s algorithm · 2 marks

Are we close to our original goal of finding an algorithm where the effort is proportional to the number of digits?

Answer: No. Euclid’s subtraction algorithm can need a number of steps proportional to the value of the numbers: gcd(99, 2) takes about 49 subtractions, and gcd(2k + 1, 2) about k.

Step-by-step solution

Idea: Find a case where subtraction is slow: subtracting a small number from a big one again and again. Count the steps and compare with the number of digits.

  1. Take gcd(99, 2). Each reduction subtracts 2: gcd(99, 2) → gcd(97, 2) → gcd(95, 2) → … → gcd(5, 2) → gcd(3, 2) → gcd(2, 1) → gcd(1, 1) → gcd(1, 0), giving the answer 1. Going from 99 down to 1 in steps of 2 takes 49 subtractions, and then 2 more: about 50 reductions for a 2-digit number.1 mark
  2. In general gcd(2k + 1, 2) needs about k reductions. So gcd(999, 2) would need about 500, and gcd(9999, 2) about 5000: one more digit makes the work about 10 times larger. The number of steps is proportional to the value of the number, not to the number of digits.½ mark
  3. So no, we are not close to the goal yet. (Replacing repeated subtraction by division, using the remainder, fixes this.)½ mark
No. With repeated subtraction, gcd(2k + 1, 2) takes about k reductions (about 49 for gcd(99, 2)), so the effort grows with the value of the number, not with its number of digits.

Answer to write in the exam

gcd(99, 2) → gcd(97, 2) → gcd(95, 2) → … → gcd(3, 2) → gcd(2, 1) → gcd(1, 1) → gcd(1, 0) = 1

99 → 1 in steps of 2: 49 subtractions (+ 2 more)

gcd(2k + 1, 2) needs about k reductions, e.g. gcd(9999, 2) about 5000

Steps ∝ value of the number, not number of digits

∴ No, we are not close to the goal.

Common mistakes that cost marks

  • Judging from the gcd(375, 825) case alone (only 7 reductions). One fast case does not show the algorithm is always fast.
  • Saying the number of steps grows by 1 for each extra digit. For gcd(2k + 1, 2) it grows about 10 times for each extra digit.
  • Forgetting the last reductions gcd(2, 1) → gcd(1, 1) → gcd(1, 0) when counting.

How this can come in the exam

MCQ (1 mark)

Using Euclid’s subtraction algorithm, about how many reductions does gcd(201, 2) need?

  1. 2
  2. 3
  3. About 100
  4. About 200
Show answer

(C) About 100
201 = 2 × 100 + 1, so k = 100: 2 is subtracted 100 times to reach gcd(2, 1), then 2 more steps. About 100 reductions.

Try one yourself

How many times is 2 subtracted from 51 before the subtraction algorithm reaches gcd(2, 1)?

Show answer

51 → 49 → … → 3 → 1: (51 − 1) ÷ 2 = 25 subtractions.

More questions like this

All Algorithms questions · All maths questions