How to rewrite Ackermann function in non-recursive style?

java, non-recursive

Solution

Not quite O(1) but definitely non-recursive.

public static int itFunc(int m, int n){
    Stack<Integer> s = new Stack<Integer>;
    s.add(m);
    while(!s.isEmpty()){
        m=s.pop();
        if(m==0||n==0)
            n+=m+1;
        else{
            s.add(--m);
            s.add(++m);
            n--;
        }
    }
    return n;
}

Problem

I have function ``` public static int func(int M,int N){ if(M == 0 || N == 0) return M+N+1; return func(M-1, func(M, N-1)); } ``` How to rewrite it in non-recursive style ? Maybe, is it implementation some algorithm?

Original source

Related problems