I try to write a recursive solution that calculates Ackerman's function:
For the values of m
and n
given, the function of Ackerman is described as follows:
A(m,n) = n+1 , si m = 0
A(m,n) = A(m-1,1) , si n = 0
A(m,n) = A(m-1,A(m,n-1)) , si m > 0 y n > 0
The code that I implemented is the following, although it gives me a stack over flow
public class Ackeman{
public int ackerman(int m, int n){
int acker = 0;
if(m==0){
acker += n+1;
}else{
if(n == 0){
acker += ackerman(m-1,1);
}else if(m > 0 && n > 0){
acker += ackerman(m-1,ackerman(m,n-1));
}
}
return acker;
}
}