2

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;
                }
            }
        }
    }
}
  • My guess is that there is an error in your solution. Edit it into your question and we can take a look. – Degustaf Jan 06 '15 at 20:06
  • Also, iteration and recursion are merely two different approaches to solving a problem. There are very few, if any, problems with a recursive solution that do not have an equivalent iterative solution (but possibly requiring some auxiliary data structure to take the place of the recursion stack). – twalberg Jan 06 '15 at 20:57
  • That was the point in the question, I was led to believe that the Ackermann function was a problem which couldn't have a iterative solution. I first heard of the Ackermann function from this video [link](https://www.youtube.com/watch?v=i7sm9dzFtEI) where he clearly states that it could only be defined recursively. – Morgan Gethin Barrett Jan 06 '15 at 21:05
  • It's *defined* recursively. That's not the same thing as "can only be evaluated recursively". It just means there's no way to define the value of the function given a specific input without referring to the value of the function with other inputs. – twalberg Jan 06 '15 at 21:16

1 Answers1

2

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.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Jon Pearce
  • 36
  • 3