ProgrammingHardInfosys · Capgemini pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer Function f(Integer x, Integer y)
    if (x > y) return f(x - y, y)
    if (x < y) return f(x, y - x)
    return x
End Function
Print f(48, 18)
  1. A. 6
  2. B. 18
  3. C. 3
  4. D. 12

Correct answer: A. 6

Subtraction form of Euclid: (48, 18) → (30, 18) → (12, 18) → (12, 6) → (6, 6) → 6 = gcd. It is correct but can take O(max) steps (e.g. f(1000, 1)), unlike the mod version's O(log).

More Pseudocode and Code Tracing questions

Exams that ask this topic