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.
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)
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! + ....
Describe the structure and function of the electoral college. •How and when was it created in...
Describe the structure and function of the electoral college. •How and when was it created in the U.S.? •Why was it created, and by whom?
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 ? ??,...
8. Definition: A set A is finite if there exists a non-negative integer c such that...
8. Definition: A set A is finite if there exists a non-negative integer c such that there exists a bijection from A to {n ∈ N : n ≤ c}. (The integer c is called the cardinality of A.) (a) Let A be a finite set, and let B be a subset of A. Prove that B is finite. (Hint: induction on |A|. Note that our proof can’t use induction on |B|, or indeed refer to “the number of elements...
Let A be a non-negative random variable (A>0) a) If A is discrete, show that E[A]...
Let A be a non-negative random variable (A>0) a) If A is discrete, show that E[A] >= 0 b) If A is continous , show E[X]>=0
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT