I have had a long time trying to convert this function into a loop but I can't find the way to do it. I have started with a while(m != 0)
and then the conditionals inside, but the third if is the one that won't let me do it.
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;
}
Also, I don't know is I need to use a BigInteger, aretrograde and inverteds the program will run StackOverFlow Error
and won't let me know if I need it.
Edits:
1st:
m cannot be less than zero in the input, that will never happen. Same situation with n, the code doesn't need to handle that.
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.