(IN JAVA) Ackermann’s function, named after the Greek mathematician Wilhelm Acke
ID: 3776852 • 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
Hi, Please find my implementation.
Please let me know in case of any issue.
public class Ackermann {
public static int ackermann(int x, int y){
// BAse case 1
if(x == 0)
return 2*y;
// Base Case 2
if(x >=1 && y == 0)
return 0;
// Base Case 3
if(x >= 1 && y==1)
return 2;
// recursive call
return ackermann(x-1, ackermann(x, y-1));
}
public static void main(String[] args) {
System.out.println("Ackermann(3,4): "+ackermann(3, 4));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.