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 + 1G(m, 0) = G(m - 1, 1)form > 0G(m, n) = G(m - 1, G(m, n - 1))form > 0andn > 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(andn <= 50whenm <= 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