ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function A(Integer m, Integer n)
    if (m == 0)
        return n + 1
    end if
    if (n == 0)
        return A(m - 1, 1)
    end if
    return A(m - 1, A(m, n - 1))
End Function

Print A(2, 3)
  1. A. 7
  2. B. 9
  3. C. 6
  4. D. 11

Correct answer: B. 9

This is the Ackermann function. A(1, n) = n + 2 and A(2, n) = 2n + 3, so A(2, 3) = 9. (Check: A(2, 0) = A(1, 1) = 3, A(2, 1) = A(1, 3) = 5, A(2, 2) = 7, A(2, 3) = 9.)

More Pseudocode and Code Tracing questions

Exams that ask this topic