Question

In: Computer Science

Ackermann’s function is a recursive mathematical algorithm that can be used to test how well a...

Ackermann’s function is a recursive mathematical algorithm that can be used to test how well a computer performs recursion. Write a method ackermann(m, n), which solves Ackermann’s function. Use the following logic in your method:

If m = 0, then return n + 1

If n = 0, then return ackermann(m-1, 1)

Otherwise, return ackermann(m -1, ackermann(m, n - 1))

Test your method in a java program that displays the return values of the following method calls:

ackermann(0, 0), ackermann(0, 1), ackermann(1, 1), ackermann(1, 2) ackermann(1, 3), ackermann(2, 2), ackermann(3, 2)

Solutions

Expert Solution

Explanation:

Here is the code which has the method ackermann taking the integer values m and n.

Then the if conditions are applied using recursion as given in the pseudocode above.

Then the given calls are made.

Code:


public class Main
{
public static int ackermann(int m, int n)
{
if(m==0)
return n+1;
  
if(n==0)
return ackermann(m-1, 1);
  
return ackermann(m-1, ackermann(m, n-1));
}
  
   public static void main(String[] args) {
      
       System.out.println(ackermann(0, 0));
       System.out.println(ackermann(0, 1));
       System.out.println(ackermann(1, 1));
       System.out.println(ackermann(1, 2));
       System.out.println(ackermann(1, 3));
       System.out.println(ackermann(2, 2));
       System.out.println(ackermann(3, 2));
   }
}

output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

PLEASE COMMENT IF YOU NEED ANY HELP!


Related Solutions

C++ Ackermann’s function is a recursive mathematical algorithm that can be used to test how well...
C++ Ackermann’s function is a recursive mathematical algorithm that can be used to test how well a computer performs recursion. Write a function A(m, n) that solves Ackermann’s function. Use the following logic in your function: If m = 0 then      return n + 1 If n = 0 then       return A(m−1, 1) Otherwise,          return A(m–1, A(m, n–1)) Test your function in a driver program that displays the following values: A(0, 0)   A(0, 1)   A(1, 1)    A(1,...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays of integers. Neither list contains duplicates, and the resulting list should not contain duplicates either. Hint: You may want to call a helper function from merge. PROGRAM: C
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;...
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;   f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2 b) Solve f(n) as a function of n using the methodology used in class for Homogenous Equations. Must solve for the constants as well as the initial conditions are given.
Exercises a - b refer to the recursive algorithm SelectionSort (a.) In one part of algorithm...
Exercises a - b refer to the recursive algorithm SelectionSort (a.) In one part of algorithm SelectionSort, the index of the maximum item in a list must be found. This requires comparisons between list elements. In an n-element (unsorted) list, how many such comparisons are needed in the worst case to find the maximum element? How many such comparisons are needed in the average case? (b.) Defining the basic operation as the comparison of list elements and ignoring the amount...
Explain the benefits a recursive algorithm can provide. Discuss the negative aspects of using recursion.
Explain the benefits a recursive algorithm can provide. Discuss the negative aspects of using recursion.
There are two restrictions on the type of grammars that can be used with a recursive...
There are two restrictions on the type of grammars that can be used with a recursive descent parser. The first is that the grammar cannot have any left recursive productions. Give an example of a left recursive production and explain why such productions would be a problem. The second restriction is that the grammar must not require more than one token look ahead. Give an example of a production that does not have this property. Explain why this restriction is...
Iterative implementation of a recursive algorithm executes faster than recursive implementation because no _____ needs to...
Iterative implementation of a recursive algorithm executes faster than recursive implementation because no _____ needs to be maintained. Select one: a. recursion b. iteration c. recurrence d. stack e. array
Spirometry is a common test used to assess how well your lungs work, by measuring how...
Spirometry is a common test used to assess how well your lungs work, by measuring how much air you can inhale, how much you can exhale, and how quickly you can exhale. It can be used to diagnose chronic obstructive pulmonary disease (COPD) (see, for example, Schneider et al., 2009). We want to study the effectiveness of spirometry in diagosing COPD. Suppose we gave the test to 400 people with COPD and found that 337 tested positive for COPD. We...
how well can Ovid the Poet be used as a historian of the region in his...
how well can Ovid the Poet be used as a historian of the region in his time?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT