A basic tool for visually tracing the recursive call stack for the Ackermann–Péter function.
g++ -std=c++11 -o ackermann_visualizer ackermann_visualizer.cpp
clang++ -Wall -std=c++11 ackermann_visualizer.cpp -o ackermann_visualizer
ackermann_visualizer m n [filename]
- m: positive integer m in A(m, n)
- n: positive integer n in A(m, n)
- filename: optionally output to a file instead of console
ackermann_visualizer 2 2
Output:
A(1, 2)
A(0, A(1, 1))
A(0, A(0, A(1, 0)))
A(0, A(0, A(0, 1)))
A(0, A(0, 2))
A(0, 3)
Value: 4
Number of calls: 6
Run time: 0.000000 seconds
This program may crash with high enough input values due to a recursion stack overflow. The Ackermann call stack grows very quickly.
Examining the definition of the function, Let RA(m, n) define the number of calls to A(m, n).
It can be concluded through some proof that:
So for example:
So, the number of calls for A(3, 6):
And even crazier:
It is then evident how quickly one can hit the limit of the stack size even for small integer input. Also, I doubt most of the output would be useful visually for computations with that level of recursion. This program is however quite nice for input that produces smaller output.