Problem 449101 · hard · Phase 04 Non-Linear Data Structures

Nested Growth Function

recursion · memoisation · Ackermann function

A research lab benchmarks its interpreters with a famously fast-growing two-argument function G, defined for non-negative integers by:

  • G(0, n) = n + 1
  • G(m, 0) = G(m - 1, 1) for m > 0
  • G(m, n) = G(m - 1, G(m, n - 1)) for m > 0 and n > 0

Write growth(m, n) returning G(m, n) for the small inputs in the constraints.

Examples

Input:  m = 2, n = 3
Output: 9

Input:  m = 0, n = 5
Output: 6

Constraints

  • 0 <= m <= 3, 0 <= n <= 6 (and n <= 50 when m <= 2)
  • With memoisation the recursion depth stays under 200 for these inputs; without it, the call count for G(3, 6) is in the hundreds of thousands and the depth is well under the limit but the time adds up.

Goals

  • Implement a recursion whose argument is itself a recursive call
  • Memoise a two-argument function to tame an explosive call tree
  • Respect small input bounds and recursion depth
Starting Python…