# Ackermann function (C)

The **Ackermann function** or **Ackermann-Péter function** is defined recursively for non-negative integers *m* and *n* as follows:

In the theory of computation, the Ackermann function is a simple example of a recursive function that is not primitively recursive. Note that this function grows very quickly -- even `A(4, 3)`

cannot be feasibly computed on ordinary computers.

This article explains a few different methods of computing the Ackermann function in C. Of course, since most values of the Ackermann function exceed the size of a machine word (even a 64-bit word), these implementations are more useful for illustration than practical computation of Ackermann function values.

## Contents |

## [edit] Directly from definition

The most basic implementation is based directly on the definition above, resembling it nearly identically, except that the greater-than-zero conditions are implicit due to previous conditions being false. We use unsigned types to avoid the possibility of illegal negative inputs.

<<ackermann implementations>>=unsignedintnaive_ackermann(unsignedintm,unsignedintn){calls++;if(m == 0)returnn + 1;elseif(n == 0)returnnaive_ackermann(m - 1, 1);elsereturnnaive_ackermann(m - 1, naive_ackermann(m, n - 1));}

## [edit] Partially iterative

For compilers that do not eliminate tail recursion, we can turn two of the recursive calls above into iterative constructs. This greatly reduces the number of recursive calls, although the number is still prohibitive even for small inputs.

<<ackermann implementations>>=unsignedintiterative_ackermann(unsignedintm,unsignedintn){calls++;while(m != 0){if(n == 0){n = 1;}else{n = iterative_ackermann(m, n - 1);}m--;}returnn + 1;}

## [edit] Precomputed for small *m*

It is possible to prove the following useful formulas for small values of the input *m*:

*A*(0,*n*) = *n* + 1

*A*(1,*n*) = *n* + 2

*A*(2,*n*) = 2*n* + 3

*A*(3,*n*) = 2^{n + 3} − 3

Using these, we can drastically reduce runtime not only for these inputs, but for larger inputs which make recursive calls with these inputs. We perform the exponentiation using shifting:

<<ackermann implementations>>=unsignedintformula_ackermann(unsignedintm,unsignedintn){calls++;while(1){switch(m){case0:returnn + 1;case1:returnn + 2;case2:return(n << 1) + 3;case3:return(1 << (n+3)) - 3;default:if(n == 0){n = 1;}else{n = formula_ackermann(m, n - 1);}m--;break;}}}

This doesn't quite work correctly for *n* + 3 equal to the word size, for which `1 << (n+3)`

is undefined due to overflow, but we ignore this here.

## [edit] Test main

We write a test driver that prints a requested value of the Ackermann function using all three implementations, as well as the number of calls made by each:

<<ackermann.c>>=#include <stdio.h>#include <stdlib.h> /* atoi() */staticunsignedintcalls; ackermann implementationsintmain(intargc,char* argv[]){unsignedintm, n, result; m = (unsigned)atoi(argv[1]); n = (unsigned)atoi(argv[2]); calls = 0; result = naive_ackermann(m, n); printf("Naive: %u (%u calls)\n", result, calls); calls = 0; result = iterative_ackermann(m, n); printf("Iterative: %u (%u calls)\n", result, calls); calls = 0; result = formula_ackermann(m, n); printf("Formula: %u (%u calls)\n", result, calls);return0;}

Here's some sample input/output:

$ ./ackermann 4 1 Naive: 65533 (2862984010 calls) Iterative: 65533 (1431459240 calls) Formula: 65533 (2 calls)

Download code |