Question

Objectives .To gain experience with using recursion by implementing the Ackermanns Function. To gain experience with using class variables to record method invocation history Description The Ackermanns function is of interest because it grows rapidly with respect to the size of m and n. It is the simplest example of a well-defined total function which is computable but not primitive recursive. This means it cannot be implemented using only for-loops. Notice that for- loops (which have a fixed iteration limit) are a special case of while-loops. The Ackermanns function is defined recursively for nonnegative integers as follows 2+1 Acker (m-1, 1) Acker (m -1. Acker (m, n-1) if m=0 if n=0 otherwise Acker (m, n)- Implement the function as a method in Java and trace the histrory of method invocations as follows (e.g., m-1, n-2): Input two intergers separated by a space character (enter q to quit): 1 2 Enter method acker: m = 1, n = 2 Enter method acker: m = 1, n = 1 Enter method acker: m- 1, n = 0 Enter method acker: m 0, n 1 Leave method acker: acker (0, 1)2 Leave method acker: acker (1, 0) = 2 Enter method acker: m = 0, n = 2 Leave method acker: acker (0, 2) = 3 Leave method acker: acker (1, 1) = 3 Enter method acker: m = 0, n = 3 Leave method acker: acker (0, 3) = 4 Leave method acker acker (1, 2) 4 Total number of invocations = 6, result 4

Please write down the code in JAVA.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public int acker(int m, int n) {
    if(m == 0) {
        return n+1;
    } else if(n == 0) {
        return acker(m-1, 1);
    } else {
        return acker(m-1, acker(m, n-1));
    }
}
Add a comment
Know the answer?
Add Answer to:
Please write down the code in JAVA. Objectives .To gain experience with using recursion by implementing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am looking for toe menu "3" Ackermann table lookup function. IN JAVA I already made...

    I am looking for toe menu "3" Ackermann table lookup function. IN JAVA I already made my own menu, value, trace functions I just need table lookup function Ackermann's function Please create the following menu for the Ackermann project. This program allows you to call the Ackermann function. Please choose a version of Ackermann's Function. 1) Ackermann value. 2) Ackermann trace 3) Ackermann table lookup. 4) Quit playing with the Ackermann Function. Please choose one of the 4 choices Ackermann's...

  • Write the code in java programming language To get some practice with recursion. You can do...

    Write the code in java programming language To get some practice with recursion. You can do all this in one driver program. Write a recursive method to compute the result of the Fibonacci sequence: Fibonacci(N) = Fibonacci(N -1) + Fibonacci(N-2) N == 0 is 0 N == 1 is 1 Testing: Display the result for Fibonacci(N) and the number of function calls for N = 2, 5 and 10.

  • Write a section of Java code using while, do-while and for loops Write a section of...

    Write a section of Java code using while, do-while and for loops Write a section of Java code that will print out random integers between 1 and 100 while N is less than or equal to 5. Sample output > random # 1 is: 73 random # 2 is: 68 random # 3 is: 76 random # 4 is: 64

  • Please code in Java and ignore question 2, thank you! 1. Write a Java method that...

    Please code in Java and ignore question 2, thank you! 1. Write a Java method that takes as its only input parameter a single integer, n, and returns the number of unique binary search trees that can be constructed with integers 1 through n. (Hint: You'll probably want to do this recursively.) (Note: This is not asking for the number of maximally tall BSTs. The BSTs can be any height.) 2. Work through the deletion exercises in 2-4-deletion-supplementary.pdf (attached above)....

  • JAVA Restrictions 1. don't use any other way to implement a loop but recursion (while and...

    JAVA Restrictions 1. don't use any other way to implement a loop but recursion (while and for are forbidden). 2. don't use Math.pow\ 3. Pls do not use while or for 1. write a class called MultiplyByItself with a main method 2. the program asks for two integer numbers: x and n. n must be 0 or greater than 0. 3. then the program prints x raised to the n-th power. For example if x is 2 and n is...

  • Please implement it using recursion and it would be great if you could provide test code...

    Please implement it using recursion and it would be great if you could provide test code as well. Thanks. 1) Implement exponentiation recursively based on the following mathematical facts , if n = even number m" = 2 1-1 1-1 n-1 2 mxm т × m T = m × (mT) If n = o a a number , You can assume n is a non-negative integer. int expo(const int m, const unsigned int n)

  • Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number...

    Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number which is defined as follows: fibo(0) = 0 fibo(1) = 1 fibo(n) = fibo(n-1) + fibo(n-2) for n >= 2 Question 2. Write a recursive function that calculates the sum of quintics: 1power of5 + 2power of5 + 3power of5 + … + n5 Question 3. Write a program to find a route from one given position to another given position for the knight...

  • Lab 1.java only Goal: This lab will give you experience with defining and using classes and...

    Lab 1.java only Goal: This lab will give you experience with defining and using classes and fields, and with conditionals and recursive functions. Getting Started --------------- Read the Fraction.java class into a text editor and compile it filling in the command javac -g Fraction.java. The program should compile without errors. In a shell window, run the program using "java Fraction". The program should run, although it will print fractions in a non-reduced form, like 12/20. Part I: Constructors (1 point)...

  • Can you please translate this code from Java to C++; These are the only libraries i...

    Can you please translate this code from Java to C++; These are the only libraries i am allowed to use #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <string> using namespace std; 1 /* Given a string, compute recursively (no loops) a new string where all * appearances of "pi" have been replaced by "3.14". public String changepi(String str) { if(str.length() <= 1) return str; if(str.substring(0, 2).equals("pi")) return "3.14" + changepi(str.substring(2)); 11 12 return str.charAt(0) + changepi(str.substring(1)); }

  • Using Java solve recursively without the use of loops or modifying the method signatures. /** *...

    Using Java solve recursively without the use of loops or modifying the method signatures. /** * Given a sorted input array a, return a sorted array of size a.length + 1, * consisting of all elements of array a and integer i. * * @param a an array that is sorted in a non-descending order * @param i an integer * @return a sorted array of size a.length + 1, consisting of all elements of * array a and integer...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT