Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class AckermannMenu {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. System.out.println("This program allows you to call the Ackermann function.");
  8.  
  9. Ackermann integers = new Ackermann();
  10.  
  11. Scanner userin = new Scanner(System.in);
  12.  
  13. int count = 0;
  14.  
  15. int option;
  16.  
  17. do{
  18.  
  19. printOptions();
  20.  
  21. option = userin.nextInt();
  22.  
  23. userin.nextLine();
  24.  
  25. if (option == 1){
  26.  
  27. System.out.print("Please enter a value for m: ");
  28.  
  29. int m = userin.nextInt();
  30.  
  31. System.out.print("Please enter a value for n: ");
  32.  
  33. int n = userin.nextInt();
  34.  
  35. System.out.println("Ackermann value for m = " + m + " and n = " + n + " is " + Ackermann.Ack(m,n));
  36.  
  37. }else if (option == 2){
  38.  
  39. System.out.print("Please enter a value for m: ");
  40.  
  41. int m = userin.nextInt();
  42.  
  43. System.out.print("Please enter a value for n: ");
  44.  
  45. int n = userin.nextInt();
  46.  
  47.  
  48.  
  49. System.out.println("Ackermann value for m = " + m + " and n = " + n + " is " + Ackermann.traceAck(m, n, count));
  50.  
  51. }else if (option == 3){
  52.  
  53. }
  54.  
  55. } while(option != 4);
  56.  
  57. System.out.println("Quit!");
  58.  
  59. }
  60.  
  61.  
  62.  
  63. public static void printOptions(){
  64.  
  65. System.out.println("\nPlease choose a version of Ackermann's Function.");
  66.  
  67. System.out.println("\t1) Ackermann value.");
  68.  
  69. System.out.println("\t2) Ackermann trace");
  70.  
  71. System.out.println("\t3) Ackermann table lookup.");
  72.  
  73. System.out.println("\t4) Quit playing with the Ackermann Function.");
  74.  
  75. System.out.println("\tPlease choose one of the 4 choices.");
  76.  
  77. }
  78.  
  79. }
  80.  
  81. class Ackermann {
  82.  
  83. public static int Ack(int m, int n){
  84.  
  85. if(m == 0){
  86.  
  87. return n + 1;
  88.  
  89. }
  90.  
  91. else if( n == 0){
  92.  
  93. return Ack(m - 1, 1);
  94.  
  95. }
  96.  
  97. else {
  98.  
  99. return Ack(m - 1, Ack(m, n - 1));
  100.  
  101. }
  102.  
  103. }
  104.  
  105.  
  106.  
  107. public static int traceAck(int m, int n, int count){
  108.  
  109. System.out.println("count = " + count + " m = " + m + " n = " + n);
  110.  
  111.  
  112. if(m==0){
  113. count++;
  114. return n+1;
  115.  
  116. }
  117.  
  118. else if(n==0){
  119. count++;
  120. return traceAck(m - 1, 1, count);
  121.  
  122. }
  123.  
  124. else{
  125. count++;
  126. return traceAck(m-1, traceAck(m, n - 1, count),count);
  127.  
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement