Data Structures & AlgorithmsModerateCodeforces Div2 A · number theory

Problem: How many iterations does Euclid's algorithm take for gcd(1071, 462)? Count the loop passes of the code.

Problem: How many iterations does Euclid's algorithm take for gcd(1071, 462)? Count the loop passes of the code.
a, b = 1071, 462
steps = 0
while b:
    a, b = b, a % b
    steps += 1
print(steps)
What is printed?
  1. A. 4
  2. B. 2
  3. C. 5
  4. D. 3

Correct answer: D. 3

The remainders are 147, 21 and 0, so the loop runs three times. The gcd is 21.

More Coding Interview Problems questions

Exams that ask this topic