Computation by LOOP program The functions \operatorname{A}_i fit into the (finite-level)
fast-growing hierarchy (FGH) of functions : \begin{array}{lcl} \operatorname{FGH}_{0}(n) & = & n+1 \\ \operatorname{FGH}_{m+1}(n) & = & \operatorname{FGH}_{m}^{n}(n) \, . \end{array} The following inequality holds: \forall m > 1, \forall n > 1: \operatorname{A}_m(n) For fixed k, the function \operatorname{FGH}_{k}(n) can be computed by a
LOOP program of nesting depth k: : • INPUT (n) LOOP n: # nesting depth: 1 LOOP n: # nesting depth: 2 ... # ... LOOP n: # nesting depth: k n += 1 # • OUTPUT (n) The function \operatorname{A}_k(n) can also be computed by a LOOP-k program. (The program (schema) is not listed here.) It is obvious that \operatorname{A}(m,n), not being a
primitive recursive function see below, cannot be computed by a LOOP program.
Computation by term rewriting system, based on 2-ary function The
recursive definition of the Ackermann function can naturally be transposed to a
term rewriting system (TRS). The definition of the
2-ary Ackermann function leads to the obvious reduction rules \begin{array}{lll} \text{(r1)} & A(0,n) & \rightarrow & S(n) \\ \text{(r2)} & A(S(m),0) & \rightarrow & A(m,S(0)) \\ \text{(r3)} & A(S(m),S(n)) & \rightarrow & A(m,A(S(m),n)) \end{array}
Example Compute A(1,2) \rightarrow_{*} 4 The reduction sequence is To compute \operatorname{A}(m, n) one can use a
stack, which initially contains the elements \langle m,n \rangle. Then repeatedly the two top elements are replaced according to the rules \begin{array}{lllllllll} \text{(r1)} & 0 &,& n & \rightarrow & (n+1) \\ \text{(r2)} & (m+1) &,& 0 & \rightarrow & m &,& 1 \\ \text{(r3)} & (m+1) &,& (n+1) & \rightarrow & m &,& (m+1) &,& n \end{array} Schematically, starting from \langle m,n \rangle:
WHILE stackLength <> 1 {
POP 2 elements;
PUSH 1 or 2 or 3 elements, applying the rules r1, r2, r3 } The
pseudocode is published in . For example, on input \langle 2,1 \rangle,
Remarks • The leftmost-innermost strategy is implemented in 225 computer languages on
Rosetta Code. • For all m,n the computation of A(m,n) takes no more than (A(m,n) + 1)^m steps. • pointed out that in the computation of \operatorname{A}(m,n) the maximum length of the stack is \operatorname{A}(m,n), as long as m>0. Their own algorithm, inherently iterative, computes \operatorname{A}(m,n) within \mathcal{O}(m \operatorname{A}(m,n)) time and within \mathcal{O}(m) space.
Computation by TRS, based on iterated 1-ary function The definition of the iterated
1-ary Ackermann functions leads to different reduction rules \begin{array}{lll} \text{(r4)} & A(S(0),0,n) & \rightarrow & S(n) \\ \text{(r5)} & A(S(0),S(m),n) & \rightarrow & A(S(n),m,S(0)) \\ \text{(r6)} & A(S(S(x)),m,n) & \rightarrow & A(S(0),m,A(S(x),m,n)) \end{array} As function composition is associative, instead of rule r6 one can define \begin{array}{lll} \text{(r7)} & A(S(S(x)),m,n) & \rightarrow & A(S(x),m,A(S(0),m,n)) \end{array} Like in the previous section the computation of \operatorname{A}^1_m(n) can be implemented with a stack. Initially the stack contains the three elements \langle 1,m,n \rangle. Then repeatedly the three top elements are replaced according to the rules this computation is more efficient in that respect.
Computation by TRS, based on hyperoperators As — or — showed explicitly, the Ackermann function can be expressed in terms of the
hyperoperation sequence: A(m,n) = \begin{cases} n+1 & m=0 \\ 2[m](n+3) - 3 & m>0 \\ \end{cases} or, after removal of the constant 2 from the parameter list, in terms of Buck's function A(m,n) = \begin{cases} n+1 & m=0 \\ F(m,n+3) - 3 & m>0 \\ \end{cases} Buck's function \operatorname{F}(m,n) = 2[m]n, a variant of Ackermann function by itself, can be computed with the following reduction rules: \begin{array}{lll} \text{(b1)} & F(S(0),0,n) & \rightarrow & S(n) \\ \text{(b2)} & F(S(0),S(0),0) & \rightarrow & S(S(0)) \\ \text{(b3)} & F(S(0),S(S(0)),0) & \rightarrow & 0 \\ \text{(b4)} & F(S(0),S(S(S(m))),0) & \rightarrow & S(0) \\ \text{(b5)} & F(S(0),S(m),S(n)) & \rightarrow & F(S(n),m,F(S(0),S(m),0)) \\ \text{(b6)} & F(S(S(x)),m,n) & \rightarrow & F(S(0),m,F(S(x),m,n)) \end{array} Instead of rule b6 one can define the rule \begin{array}{lll} \text{(b7)} & F(S(S(x)),m,n) & \rightarrow & F(S(x),m,F(S(0),m,n)) \end{array} To compute the Ackermann function it suffices to add three reduction rules \begin{array}{lll} \text{(r8)} & A(0,n) & \rightarrow & S(n) \\ \text{(r9)} & A(S(m),n) & \rightarrow & P(F(S(0),S(m),S(S(S(n))))) \\ \text{(r10)} & P(S(S(S(m)))) & \rightarrow & m \\ \end{array} These rules take care of the base case A(0,n), the alignment (n+3) and the fudge (-3).
Example Compute A(2,1) \rightarrow_{*} 5 The matching equalities are • when the TRS with the reduction rule \text{b6} is applied: \begin{align} & A(2,1) +3 = F(2,4) = \dots = F^6(0,2) = F(0,F^5(0,2)) = F(0,F(0,F^4(0,2))) \\ & = F(0,F(0,F(0,F^3(0,2)))) = F(0,F(0,F(0,F(0,F^2(0,2))))) = F(0,F(0,F(0,F(0,F(0,F(0,2)))))) \\ & = F(0,F(0,F(0,F(0,F(0,3))))) = F(0,F(0,F(0,F(0,4)))) = F(0,F(0,F(0,5))) = F(0,F(0,6)) = F(0,7) = 8 \end{align} • when the TRS with the reduction rule \text{b7} is applied: \begin{align} & A(2,1) +3 = F(2,4) = \dots = F^6(0,2) = F^5(0,F(0,2)) = F^5(0,3) = F^4(0,F(0,3)) = F^4(0,4) \\ & = F^3(0,F(0,4)) = F^3(0,5) = F^2(0,F(0,5)) = F^2(0,6) = F(0,F(0,6)) = F(0,7) = 8 \end{align}
Remarks • The computation of \operatorname{A}_{i}(n) according to the rules {b1 - b5, b6, r8 - r10} is deeply recursive. The maximum depth of nested Fs is A(i,n)+1. The culprit is the order in which iteration is executed: F^{n+1}(x) = F(F^{n}(x)). The first F disappears only after the whole sequence is unfolded. • The computation according to the rules {b1 - b5, b7, r8 - r10} is more efficient in that respect. The iteration F^{n+1}(x) = F^{n}(F(x)) simulates the repeated loop over a block of code. The nesting is limited to (i+1), one recursion level per iterated function. showed this correspondence. • These considerations concern the recursion depth only. Either way of iterating leads to the same number of reduction steps, involving the same rules (when the rules b6 and b7 are considered "the same"). The reduction of A(2,1) for instance converges in 35 steps: 12 × b1, 4 × b2, 1 × b3, 4 × b5, 12 × b6/b7, 1 × r9, 1 × r10. The
modus iterandi only affects the order in which the reduction rules are applied. • A real gain of execution time can only be achieved by not recalculating subresults over and over again.
Memoization is an optimization technique where the results of function calls are cached and returned when the same inputs occur again. See for instance . published a cunning algorithm that computes A(i,n) within \mathcal{O}(i A(i,n)) time and within \mathcal{O}(i) space.
Huge numbers To demonstrate how the computation of A(4, 3) results in many steps and in a large number: \begin{align} A(4, 3) & \rightarrow A(3, A(4, 2)) \\ & \rightarrow A(3, A(3, A(4, 1))) \\ & \rightarrow A(3, A(3, A(3, A(4, 0)))) \\ & \rightarrow A(3, A(3, A(3, A(3, 1)))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(3, 0))))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(2, 1))))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(1, A(2, 0)))))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(1, A(1, 1)))))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(1, A(0, A(1, 0))))))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(1, A(0, A(0, 1))))))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(1, A(0, 2)))))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(1, 3))))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(0, A(1, 2)))))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(0, A(0, A(1, 1))))))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(0, A(0, A(0, A(1, 0)))))))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(0, A(0, A(0, A(0, 1)))))))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(0, A(0, A(0, 2)) )) )) ) \\ & \rightarrow A(3, A(3, A(3, A(2, A(0, A(0, 3)))))) \\ & \rightarrow A(3, A(3, A(3, A(2, A(0, 4))))) \\ & \rightarrow A(3, A(3, A(3, A(2, 5)))) \\ & \qquad\vdots \\ & \rightarrow A(3, A(3, A(3, 13))) \\ & \qquad\vdots \\ & \rightarrow A(3, A(3, 65533)) \\ &\qquad\vdots \\ & \rightarrow A(3, 2^{65536} - 3) \\ &\qquad\vdots \\ & \rightarrow 2^{2^{65536}} - 3. \\ \end{align} == Table of values ==