GATE Computer ScienceModerateGATE CS (PYQ pattern)

Euclid's algorithm gcd(a, b) repeatedly replaces (a, b) by (b, a mod b) until b = 0, counting one step per mod operation. Over all pairs 1 ≤ b < a ≤ 1000, what is the maximum number of steps?

  1. A. 15
  2. B. 13
  3. C. 14
  4. D. 10

Correct answer: C. 14

By Lamé's theorem the worst inputs are consecutive Fibonacci numbers; the largest pair within 1000 is (987, 610) = (F₁₆, F₁₅), which needs 14 steps (610, 377, …, 1, 0). A pair needing 15 steps would require a ≥ F₁₇ = 1597. An exhaustive check of all pairs confirms 14, attained only by (987, 610).

More Algorithms questions

Exams that ask this topic