How to rewrite Ackermann function in non-recursive style?
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? 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; } This looks like homework, so I won't give you the answer but I will lead you in the right direction: If you want to breakdown the recursion, it might