Ackerman's function

1

hello I have a problem entering the "m" in the function of ackerman, with the "n" everything is fine, however, entering the value of "m" as a number equal to or greater than 5 marks me error

package ackerman;
import java.util.Scanner;
import java.math.BigInteger;
/**
*
* @author 
*/
public class Ackerman {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    int m = 0;
    int n = 0;

    BigInteger result;
    Scanner input = new Scanner (System.in); 
    System.out.println("Introduce m:");
    m = Integer.parseInt(input.nextLine());
    System.out.println("Introduce m:");
    n = Integer.parseInt(input.nextLine());
    result = Ackerman(m,n);
    System.out.println("Ack(" + m + "," + n + ") = " + result);
}

private static BigInteger Ackerman(long m,long n){
    BigInteger res = new BigInteger(String.valueOf(0));
    if(m==0){
        res = new BigInteger(String.valueOf(n+1));
    }
    else if(m ==1){
        res = new BigInteger(String.valueOf(n+2));
    }
    else if(m ==2){
        res = new BigInteger(String.valueOf((2*n) +3));
    }
    else if(m ==3){
        res = new BigInteger(String.valueOf(Math.round(Math.pow(2, n+3) - 3)));
    }
    else if(n ==0){
        res = Ackerman(m-1,1);
    }
    else{
        res = Ackerman(m-1,Ackerman(m,n-1).longValue());
    }
    return res;
 }   
}
    
asked by Gaby C. 02.12.2016 в 02:38
source

1 answer

0

This was recently seen in another question . The solution to the calculation of the function is the question and the solution to the overflow is to mark a limit to not exceed the capacity of the machine.

If it is not enough you should cut the recursion to empty the memory in a known state and continue.

    
answered by 02.12.2016 в 08:46