Ackermann function in C

The Ackermann function is a two-parameter function that accepts non-negative integer inputs and returns a non-negative integer. While it may appear to be deceptively simple, this function has amazing growth rates that are beyond the capability of usual computing methodologies.

The Ackermann function, named after German mathematician Wilhelm Ackermann, is a recursively mathematical equation that accepts two non-negative numbers as inputs and outputs a non-negative integer. Recursion is used to implement the Ackermann function in C.

The function description:

The base cases:

  • If l is equal to zero, the method returns m + 1.
  • If m is 0, the function executes a recursive call, decrementing l by 1 and setting m to 1.

The recursive case is as follows:

  • If neither l nor m is zero, the function performs a recursive call, with l decremented by one and m set to the result of the previous recursive call, with l and m decreased by one.

The Ackermann function is defined as:

A(a,b) = b+1; if a==0

A(a,b) =A(a-1,1); if a>0 and b==0

A(a,b) = A(a-1,A(a,b-1) ); if if a > 0 and b > 0

The Ackermann function is well-known for its fast development rate, even for small inputs. As the numbers of a and b rise, so do the number of recursive calls and the level of difficulty of the computation. This rise in value makes it a difficult function to estimate for bigger values of a and b.

Algorithm:

This algorithm's Analysis:

  • This approach has a time complexity of O(mA(a, b)) for calculating A(a, b).
  • This approach has a space complexity of O(a) to compute A(a, b).

Example:

Let us take an example to illustrate the Ackermann Function in C.

Explanation:

  • ackFun(int a, int b). This function computes the Ackermann function iteratively.
  • It complies with the definition of the Ackermann function, which includes three basic cases:
    1. If a = 0, it results in b + 1.
    2. If an is larger than 0 and b is equal to 0, the process repeats with a-1 and 1.
    3. If a and b are both larger than 0, the program recurses with a-1 and the result of a different Ackermann call with a and b-1.
  • For some instances, this function does not have a return statement, which might result in undefined behavior. It must provide a value for each potential route.
  • main(): Calls ackFun(6, 4) to compute the Ackermann value with a = 6 and b = 4.
  • The result is saved as a variable value.
  • Prints the number using the printf() function.