CMPS 144L Lab Activity
Exponentiation, Recursively

An efficient approach to computing a (real number) base x raised to a (nonnegative integer) power n follows from this recursive characterization of xn:

xn = { 1   if n = 0
(x2)n/2   if n>0 and n is even
xn-1 · x   otherwise

Using this as a model, supply the body of the xToTheNRec() method in the ExponentiationTester Java application that is provided. (Alternatively, or in addition, supply the body of xToTheNRecNarrated(), which computes the same function but which, in doing so, narrates its own execution by printing messages reporting its incoming parameter values and outgoing result.)

Note: Obviously, the instructions in the previous paragraph preclude the use of the Math.pow() in your solution. End of note.

Use the application to test your method for correctness. (Make sure to have the main() method call the version of the method that you worked on, rather than the other one.) Entering a negative exponent at the prompt will cause the program to terminate.

For each x and n that the user enters, the program will display the value computed by your method as well as that computed by the Math.pow() method. If the two values are not very close, it is a strong indication that your method is in error.

The program also displays the number of multiplication operations performed by your method. (Well, you are responsible for updating the relevant variable appropriately within your method to keep an accurate count.) If your method is a faithful implementation of the recursive equation given above, the number of multiplications should be at most 2×lg(n) (i.e., twice the logarithm, to the base 2, of the exponent). (For example, with an exponent of 1000, the number of multiplications should be at most 20.) Compare that to n−1, which is the number of multiplications that you would perform by following the "naive" approach.