(IN JAVA) Ackermann’s function, named after the Greek mathematician Wilhelm Acke
ID: 3776718 • Letter: #
Question
(IN JAVA)
Ackermann’s function, named after the Greek mathematician Wilhelm Ackermann, is used in the theory of recursive functions. There are several variants of this function. Their common properties are that the function takes two parameters (x and y) and grows very fast (much faster than polynomials or exponentials). Here is one variant:
1. If x = 0, then Ackermann (x, y) = 2y.
2. If x >= 1 and y = 0, then Ackermann(x, y) = 0.
3. If x >= 1 and y = 1, then Ackermann(x, y) = 2.
4. If x >= 1 and y >= 2, then Ackermann(x, y) = Ackermann(x-1, Ackermann(x, y – 1)).
Implement this variant of Ackermann’s function with a recursive method.
Explanation / Answer
/* Function implementing Ackermann Function *
public static int ack(int x, int y)
{
if(x == 0)
{
return(2y);
}
else if(x >= 1 && y == 0)
{
return(0);
}
else if(x >=1 && y ==1);
{
return(2);
}
else if(x >=1 && y >=1);
{
return(ack(x-1, ack(x, y-1));
}
} // end of ackermann’s implementation
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.