Question: The Ackermann Function is a class recursive example in computer science. It is a function that grows very quickly (in its value and the size
The Ackermann Function is a class recursive example in computer science. It is a function that grows very quickly (in its value and the size of its called tree.) It is defined as folows:
A(m,n) = ( n + 1 if m = 0, A(m-1,1) if m > 0 and n = 0, A(m - 1, A(m,n-1)) if m > 0 and n > 0)
Complete the following program to complete the Ackermann Function:
Recursion.java:
import java.util.Scanner;
class Recursion {
public static void main(String args[]) {
int num1, num2;
long result = 0;
String aWord;
Scanner scan = new Scanner(System.in);
System.out.print("Enter two integers for testing Ackermann function: ");
num1 = scan.nextInt();
num2 = scan.nextInt();
result = Ackermann (num1,num2);
System.out.println("Ackermann(" + num1+", " + num2 + ") = " + result);
if (result < 0){
System.out.println("The returning result (-999999) is undefined from the Ackermann function");
System.out.println("due to invalid inputs. The two input integers must both be positive numbers! ");
}
System.out.print("Enter the number of people for testing handshakes function: ");
num1 = scan.nextInt();
System.out.println("handshakes(" + num1 + ") = " + handshakes(num1));
} //method main
//Complete the following two recursive methods
public static long Ackermann( long m, long n)
{ //The compiler may keep telling you the error: "Add return statement or Missing return statement",
//you should add "else return -999999;" at the end of your nested if-then-else statement
// to cover all possible cases to make the compiler happy.
} // method Ackermann
}//class Recursion
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
