ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def ack(m, n):
    if m == 0:
        return n + 1
    if n == 0:
        return ack(m - 1, 1)
    return ack(m - 1, ack(m, n - 1))
print(ack(2, 2))
  1. A. 7
  2. B. 9
  3. C. 4
  4. D. 29

Correct answer: A. 7

ack(1, n) = n + 2 and ack(2, n) = 2n + 3, so ack(2, 2) = 7. 9 is ack(2, 3), 4 is ack(1, 2), and 29 applies the m = 3 formula 2^(n+3) − 3.

More Python Programming questions

Exams that ask this topic