I've found an iterative implementation of pow(x, n) which takes o(log n) time and constant space like below:
double pow(double x, int n) {
double left = x;
double right = 1;
if (n<0) return 1/(x*pow(x,-n-1)); // Avoid binary overflow!!!!
if (!n) return 1;
while (n>1)
{
if (n%2==1) right *= left;
left = left * left;
n = n/2;
}
return left * right;
}
But I couldn't find any explanation of this algorithm. I understand the recursive solution using divide and conquer technique and I guess this solution uses similar trick. But I don't understand exactly why this works. Can anyone please explain this algorithm to me? Thanks!
This algorithm decomposes the exponent, n, into the bits which represent the number.
The first line handles negative exponents, by computing the reciprocal, and reversing the sign of n. not that it pulls x^1 out of the pow() function by subtracting one,
if (n<0) return 1/(x*pow(x,-n-1));
The algorithm handles bits that are zero by observing that x^0 = 1 (see this line):
if (!n) return 1;
The while loop is repeatedly squaring x (see left=x; left = left*left;) as it divides the exponent n by 2. The right factor is used to compute the value of the i-th bit when set.
while (n>1)
{
if (n%2==1) right *= left;
left = left * left;
n = n/2;
}
The loop terminates when n<=1, at which point the value left == x^(2*i) for the i-th bit, and right == x^(2*(i-1)) + ... x^(2*(i-n)), for the bits which are 'on'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With