Transition from recursive to iterative

I had a lot of time to convert this function into a loop, but I cannot find a way to do this. I started with while(m != 0), and then with conventions inside, but the third, if it is one that will not allow me to do this.

public static int calculatePayment(int n, int m){
    if(m == 0) return n + 1;
    if(n == 0 && m > 0) return calculatePayment(1, m - 1);
    if(n > 0 && m > 0) return calculatePayment(calculatePayment(n - 1, m), m - 1);
    return 0;
}

In addition, I do not know that I need to use BigInteger, istrograde and inverts that the program will start StackOverFlow Errorand will not let me know if I need it.

Editing:

the first:

m cannot be less than zero in the input, which will never happen. In the same situation with n, the code does not need this.

So far I have this:

while(m > 0){
             if(n == 0 && m > 0){n = 1; m--;}
             if(n > 0 && m > 0){m--; n = IDONTKNOWWHAT;}//here n is the value of
                                                        //the payment if n = n - 1
                                                        //and m = m
                                                        //here is the big deal.
            }
if(m == 0) return n + 1;   //or print

Also, the code cannot be reduced to a mathematical formula, I tried.

+3
source share
1 answer

, . ​​:) , ?

, , : Ackermann ? p >

, . BigInteger, . . , , , StackOverflow, google memoization.

+2

All Articles