Who to talk to about non recursive Ackermann function?

1.3k Views Asked by At

I have written a non recursive solution to the Ackermann function, it seems to work perfectly and work faster than the common recursive solution. So I am confused as to why it is a non primitive recursive function if it can be solved iteratively? Could anyone tell me if I have misunderstood something about what primitive recursive functions are or who should I talk to about this to get an answer?

Below is the Java code:

import java.util.Scanner;
import java.util.ArrayList;

public class ackermann {
    public static void main(String[] args){
       Scanner in = new Scanner(System.in);

        System.out.println("Enter m:");
        int m = in.nextInt();

        System.out.println("Enter n:");
        int n = in.nextInt();

        ack(m, n);
    }

    public static void ack(int inM, int inN){
        if(inM < 0 || inN < 0) return;

        ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();

        for(int m = 0; m <= inM; m++){
            arr.add(new ArrayList<Integer>());
        }

        Boolean done = false;

        while(done == false){
            for(int m = 0; m <= inM; m++){
                int n = arr.get(m).size();
                int a = 0;

                if(m == 0) a = n + 1;
                else if(n == 0){
                    if(arr.get(m - 1).size() <= 1) break;
                    a = arr.get(m - 1).get(1);
                } else {
                    int k = arr.get(m).get(n - 1);
                    if(arr.get(m - 1).size() <= k) break;
                    a = arr.get(m - 1).get(k);
                }

                arr.get(m).add(a);

                if(m == inM && n == inN){
                    System.out.println("Ack(" + inM + ", " + inN + ") = " + a);
                    done = true;
                    break;
                }
            }
        }
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

Primitive recursive functions can be implemented using only assignment, +, and definite loops. By this I mean loops of the form:

for(int i = 0; i < n; i++) { ... }

Where n is a variable that isn't changed in the loop body. To get Ackermann's function, which majorizes all primitive recursive functions, one needs to add either a goto command or indefinite loops like your while loop.