Below is my code:
public class Solution {
public double pow(double x, int n) {
if(n == 0)
return 1.0;
else if(n<0)
return 1/x * pow(x, n+1);
else
return x * pow(x, n-1);
}
}
I am getting java.lang.StackOverflowError with input (1.00001, 123456).
I tried this code on small inputs and that seemed to work. Im not sure why its erroring with large inputs, is it actually an error with the stack space, and I should use an iterative solution? I saw other posting their solution that seemed to have worked on larger power than 123456....
Thanks in advance.