Hi,
public static int mystery(int a, int b) {
if (b == 0)
return 0;
if (b % 2 == 0)
return mystery(a+a, b/2);
else{
return mystery(a+a, b/2) + a;
}
}
Can someone, please help me understand why the above code calculates
a*b ? I tried with pen and paper - don't get it, tried with debugger - same result. Tried with
System.out.println(mystery(a,b)); just before every return statement in effort to enlighten the case and I got
StackOverflowError.
Thanks in advance.
Edit: I got it why the stack overflows when I recursively call the method through System.out.println();
Edited by: blias on Apr 8, 2011 6:14 PM