Java Program Write a method that implements Ackermanns funct
Java Program:
Write a method that implements Ackermann\'s function:
Ack(x, y) = y + 1 when x = 0
Ack(x, y) = Ack(x - 1, 1) when x > 0 and y = 0
Ack(x, y) = A(x-1, A(x, y-1) when x > 0 and y > 0
Use type long. Write a program that displays the value of Ack(n, n) for n = 0, 1, 2, ... For what calue of n is the value of Ack(n, n) too large for type long? Ackermann\'s function is famous because Ack(n,n) grows so rapidly with increasing n.
Solution
package chegg;
/* Ackermann function class */
public class Ack_function
{
/* Ackermann function method take long X and Y*/
public static long Ack(long X, long Y)
{
if(X==0)
{
/* Ack(x, y) = y + 1 , when x = 0*/
return(Y + 1);
}
else
if(X > 0 && Y == 0)
{
/*Ack(x, y) = Ack(x - 1, 1) ,when x > 0 and y = 0*/
return (Ack(X-1,1));
}
else
if(X>0 && Y > 0);
{
/*Ack(x, y) = A(x-1, A(x, y-1) , when x > 0 and y > 0*/
return (Ack(X-1, Ack(X,Y-1)));
}
}
/* main method*/
public static void main(String[] args)
{
/*print the value of Ack(n, n) where n=0,1,2*/
for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
{
System.out.println(\"Ack(\"+i+\",\" + j +\") is: \"+Ack(i, j));
}
}
}
}//end of class
------------------------------------
output sample:-
Ack(0,0) is: 1
Ack(0,1) is: 2
Ack(0,2) is: 3
Ack(1,0) is: 2
Ack(1,1) is: 3
Ack(1,2) is: 4
Ack(2,0) is: 3
Ack(2,1) is: 5
Ack(2,2) is: 7
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.

