Question: Write a recursive function named RecPower that takes two positive integers x and n as input parameters and recursively computes and returns the value
Write a recursive function named RecPower that takes two positive integers x and n as input parameters and recursively computes and returns the value of x". The function takes advantage of the fact that x = x2 * x2 to break each power n down into two equal or nearly equal powers: n/2 and n-n/2 (if n is odd the two powers will not be exactly equal). The function then makes two recursive calls to compute the two smaller powers and multiplies them. The base case is n=1. Your code should be efficient enough to avoid making redundant function calls. For example, to compute 35, the function will call itself twice to compute 32 and 3. The function call that computes 32 will make a recursive call to compute 3 and then it will multiply the result by itself, while the function call that computes 3 will make two recursive calls to compute 3 and 32, and so on until the base case is reached. Obviously, you are not allowed to use the pow library function in this question.
Step by Step Solution
3.34 Rating (154 Votes )
There are 3 Steps involved in it
I have provided PYTHON CODE ... View full answer
Get step-by-step solutions from verified subject matter experts
