Ackerman Recursion

1

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;
    }
 }
    
asked by J.zer 30.10.2016 в 13:51
source

2 answers

0

I still do not know how their formats work and I'll get used to it and thanks to Mariano for editing it and to make it look more presentable. The idea is called Blue J and then I do not know how to close the topic. The question is that if the code works and the stack over flow is because I introduced very high values here a wikipedia concept ackerman function: "To give an idea of the magnitude of the values that appear from row 4 on, it can be noted that for example, A (4, 2) is greater than the number of particles that form the universe raised to the power 200 and the result of A (5, 2) can not be written since it would not fit in the physical Universe In general, below row 4, it is no longer possible to write all the digits of the result of the function. "

    
answered by 30.10.2016 / 14:23
source
2

I have tried it and I see that it is going well. If you add before return acker this line you will see that you are calculating everything as you say:

System.out.println(acker);
    
answered by 30.10.2016 в 14:13