Question

In: Computer Science

The Ackermann function is a function created by Wilhelm Ackermann in the late 1920s. For non-negative...

The Ackermann function is a function created by Wilhelm Ackermann in the late 1920s. For non-negative integers m and n, the Ackermann function is defined as

EMBED Equation.3

Write a method to recursively computer the Ackermann function. Note that the Ackermann function grows extremely quickly for even small values of m and n. Test your method with the following values but be aware that if m is greater than 3 and n is greater than 1, it will take a very long time to compute.

Solutions

Expert Solution

import java.util.Scanner;

public class HelloWorld
{
public static void main (String [] args)
{
userMenu();
}
  
public static void userMenu()
{
Scanner scan = new Scanner(System.in);
  
System.out.println("");
System.out.println("If you wish to use the ackermann function enter \"1\" \nIf you wish to exit enter \"2\"");
int userInput = scan.nextInt();
  
switch(userInput)
{
case 1: System.out.print("Enter an \"m\" value: ");
int x = scan.nextInt();
  
System.out.print("Enter a \"n\" value: ");
int y = scan.nextInt();
  
       int m = 0;
       int n = 0;
       System.out.println("\nThe Ackermann Function at \"m\" = " + m + " and \"n\" = " + n + " is: " + Ackermann(m,n));
userMenu();
  
case 2: System.out.println("");
System.out.println("Program Terminated");
System.exit(0);
  
default: System.out.println("");
System.out.println("Please enter a valid option");
userMenu();
}
}
  
public static int Ackermann(int m, int n)
{
if(n >= 0 && m >= 0)
{
if(m == 0)
{
return (n + 1);
}
  
if(m > 0 && n == 0)
{
return (Ackermann(m-1,1));
}
  
if(m > 0 && n > 0)
{
return (Ackermann((m-1),Ackermann(m,(n-1))));
}
else
{
return 0;
}
}
else
{
System.out.println("The values entered for \"x\" and \"y\" are not valid");
return 0;
}
}
}


Related Solutions

In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input,...
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input, and returns the highest sum of three neighboring elements in it. Write a main method that initializes the following five lists, gets the ThreeSum result for all of them using the above function, and prints the result to the screen. Example of the output: List 1: [4,5,4,5] , Three sum = 14 List 2: [7] , Three sum = 7 List 3: [ ]...
In MATLAB define a function named primes that takes a non-negative integer, ?, as its only...
In MATLAB define a function named primes that takes a non-negative integer, ?, as its only argument and returns a row vector containing the first ? prime numbers in order. Assume that the first prime number is 2.  Other than the zeros function, the ones function, and the colon (:) operator, you may not use any Matlab built-in array functions.
Write a function lbs2lboz(p) that takes a non-negative number, p , that represents a weight in...
Write a function lbs2lboz(p) that takes a non-negative number, p , that represents a weight in pounds and outputs a pair (l,o) such that l is an integer and p=l+o/16. Write a function oz2lboz(oz) that takes a non-negative number, oz , that represents a weight in ounces and outputs a pair (l,o) such that l is an integer and oz=l*16+o. on python
For the following Ackerman function defined for non-negative integers, write a Java program that: Contains a...
For the following Ackerman function defined for non-negative integers, write a Java program that: Contains a method called computeAckermann that takes two integer values as parameters and calculates and returns the value of the Ackerman function. The method should be defined inside a class named Ackermann. Test the implementation of Ackerman in (a) by calling it from main function and printing the returned value. The main function should be defined in a separate class named AckermannDemo.
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
1) Explain how the 1920s created growth (use examples) Cheap Labor Credit Buying on margin Advertising...
1) Explain how the 1920s created growth (use examples) Cheap Labor Credit Buying on margin Advertising 2) Explain how the above relate to the multiplier effect (make sure the concept is define)
.Variable created outside of function in the main script are available within the function. (Explain by...
.Variable created outside of function in the main script are available within the function. (Explain by giving an example) a/ is it possible to include HTML in a PHP script file?explain by giving an example here is a constant: define (“LocalHost”,”127.0.0.1”); which one of the following is the correct way to refer to the above constant? LocalHost b.$LocaolHost how many times will ”I love PHP programming!”be printed in the following program segment? $count = 0 While ($count<10) eco the PHP...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer to quit):” and store this into an int named n. If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the sum of the inverse factorials from 0 to n, that is sum = 1/0! + 1/1! + 1/2! + ....
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is the product of all positive integers less than or equal to ??. The textbook has an example of a recursive MIPS implementation of factorial. Additionally, a simplified version of the MIPS assembly language recursive implementation of the factorial function is attached. Trace the factorial example carefully using QTSPIM 2) Recursive definition of multiplication The function ??????????(??, ??) for two positive integers 1 ? ??,...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is the product of all positive integers less than or equal to ??. The textbook has an example of a recursive MIPS implementation of factorial. Additionally, a simplified version of the MIPS assembly language recursive implementation of the factorial function is attached. Trace the factorial example carefully using QTSPIM 2) Recursive definition of multiplication The function ??????????(??, ??) for two positive integers 1 ? ??,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT