Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class AckermannMenu {
- public static void main(String[] args) {
- System.out.println("This program allows you to call the Ackermann function.");
- Ackermann integers = new Ackermann();
- Scanner userin = new Scanner(System.in);
- int count = 0;
- int option;
- do{
- printOptions();
- option = userin.nextInt();
- userin.nextLine();
- if (option == 1){
- System.out.print("Please enter a value for m: ");
- int m = userin.nextInt();
- System.out.print("Please enter a value for n: ");
- int n = userin.nextInt();
- System.out.println("Ackermann value for m = " + m + " and n = " + n + " is " + Ackermann.Ack(m,n));
- }else if (option == 2){
- System.out.print("Please enter a value for m: ");
- int m = userin.nextInt();
- System.out.print("Please enter a value for n: ");
- int n = userin.nextInt();
- System.out.println("Ackermann value for m = " + m + " and n = " + n + " is " + Ackermann.traceAck(m, n, count));
- }else if (option == 3){
- }
- } while(option != 4);
- System.out.println("Quit!");
- }
- public static void printOptions(){
- System.out.println("\nPlease choose a version of Ackermann's Function.");
- System.out.println("\t1) Ackermann value.");
- System.out.println("\t2) Ackermann trace");
- System.out.println("\t3) Ackermann table lookup.");
- System.out.println("\t4) Quit playing with the Ackermann Function.");
- System.out.println("\tPlease choose one of the 4 choices.");
- }
- }
- class Ackermann {
- public static int Ack(int m, int n){
- if(m == 0){
- return n + 1;
- }
- else if( n == 0){
- return Ack(m - 1, 1);
- }
- else {
- return Ack(m - 1, Ack(m, n - 1));
- }
- }
- public static int traceAck(int m, int n, int count){
- System.out.println("count = " + count + " m = " + m + " n = " + n);
- if(m==0){
- count++;
- return n+1;
- }
- else if(n==0){
- count++;
- return traceAck(m - 1, 1, count);
- }
- else{
- count++;
- return traceAck(m-1, traceAck(m, n - 1, count),count);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement