ProgrammingHardTCS NQT · Infosys

A recursive function is written as int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }. What is gcd(1071, 462)?

  1. A. 7
  2. B. 42
  3. C. 21
  4. D. 3

Correct answer: C. 21

Euclid's steps are 1071 mod 462 = 147, 462 mod 147 = 21, 147 mod 21 = 0, so the answer is 21. The recursion depth is only logarithmic in the inputs.

More C Programming questions

Exams that ask this topic