Ackermann Function In C

The function description:

Example

int ackermannFun(int l, int m) 

{ if (l == 0) { 

 return m + 1; 

} else if (m == 0) 

{ return ackermannFun(l - 1, 1); 

 

} 

else 

{ return ackermannFun(l - 1, ackermannFun(l, m - 1)); 

 

}

}

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.

If both l and m are non-zero, the function executes a recursive function call. In this call, l is reduced by one, and m is assigned the output of the preceding recursive call where both l and m were 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 famous for its rapid growth rate, particularly evident even with modest inputs. As the values of a and b increase, the frequency of recursive calls also escalates, elevating the complexity of the computation. This escalation poses a challenge when estimating the function for larger a and b values.

Algorithm:

Example

AckermannFun(a, b)

{

 nextA and goalA are arrays indexed from 0 to a, with next[0] through next[m] being 0 and goal[0] through goal[a - l] being 1, and goal[a] being -1.

 

} 

repeat

val ← nextA[0]+ 1 

transfer ← true 

currentValue ← O 

while transfer does begin

if nextA[currentValue] = goalA[currentValue] then goalA[currentValue] ← val

else transfer ← false

nextA[currentValue] ← next[AcurrentValue]+l

currentValue ← currentValue+ 1 

end while

until nextA[a] = b+ 1 

return val {the value of Ack(a, b)}

end AckermannFun

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's consider an example to demonstrate the Ackermann Function in the C programming language.

Example

// Program to implement the Ackermann function in C

#include <stdio.h>

int ackFun(int a, int b)

{

	if (a== 0){

		return b+1;

	}

	else if((a > 0) && (b== 0)){

		return ackFun(a-1, 1);

	}

	else if((a > 0) && (b > 0)){

		return ackFun(a-1, ackFun(a, b-1));

	}

}



int main(){

	int value;

	value = ackFun(6, 4);

	printf("%d", value);

	return 0;

}

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: If a = 0, it results in b + 1. If an is larger than 0 and b is equal to 0, the process repeats with a-1 and 1. 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.
  • If a = 0, it results in b + 1.
  • If an is larger than 0 and b is equal to 0, the process repeats with a-1 and 1.
  • 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.

Input Required

This code uses input(). Please provide values below: