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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!