Question: Someone proposes the following algorithm for performing fast exponentiation, to be faster than iterative exponentiation, implemented in C++: // @x: a number. // @n: a
Someone proposes the following algorithm for performing fast exponentiation, to be faster than iterative exponentiation, implemented in C++:
// @x: a number. // @n: a positive integer. // @return x^n.
long Power(long x, unsigned int n)
{ if (n == 0)
return 1;
if (n == 1)
return x;
if (n % 2 == 0)
return Power(Power(x, 2), n / 2);
else
return x * Power(Power(x, 2), n / 2);
}
Does it work? What is its time complexity?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
