Raising a Number to a Power

Raising a Number to a Power

The more sophisticated pocket calculators allow you to raise a number to an arbi- trary power. They usually have a key labeled something like x^y, where the circum- flex indicates that x is raised to the y power. How would you do this calculation if your calculator lacked this key? You might assume you would need to multiply x by

itself y times. That is, if x was 2 and y was 8 (2 8 ), you would carry out the arithmetic for 2*2*2*2*2*2*2*2. However, for large values of y, this approach might prove tedious. Is there a quicker way?

One solution is to rearrange the problem so you multiply by multiples of 2 whenever possible, instead of by 2. Take 2 8 as an example. Eventually, we must involve eight 2s in the multiplication process. Let’s say we start with 2*2=4. We’ve used up two of the

304 CHAPTER 6 Recursion

2s, but there are still six to go. However, we now have a new number to work with:

4. So we try 4*4=16. This uses four 2s (because each 4 is two 2s multiplied together). We need to use up four more 2s, but now we have 16 to work with, and 16*16=256 uses exactly eight 2s (because each 16 has four 2s).

So we’ve found the answer to 2 8 with only three multiplications instead of seven. That’s O(log N) time instead O(N).

Can we make this process into an algorithm that a computer can execute? The scheme is based on the mathematical equality x y = (x 2 ) y/2 . In our example, 2 8 = (2 2 ) 8/2 , or 2 8 = (2 2 ) 4 . This is true because raising a power to another power is the same as multiplying the powers.

However, we’re assuming our computer can’t raise a number to a power, so we can’t handle (2 2 ) 4 . Let’s see if we can transform this into an expression that involves only multiplication. The trick is to start by substituting a new variable for 2 2 .

Let’s say that 2 2 =a. Then 2 8 equals (2 2 ) 4 , which is a 4 . However, according to the origi-

nal equality, a 4 can be written (a 2 ) 2 , so 2 8 = (a 2 ) 2 .

Again we substitute a new variable for a 2 , say a 2 =c, then (c) 2 can be written (c 2 ) 1 ,

which also equals 2 8 .

Now we have a problem we can handle with simple multiplication: c times c. You can imbed this scheme in a recursive method—let’s call it power() —for calculat-

ing powers. The arguments are x and y, and the method returns x y . We don’t need to worry about variables like a and c anymore because x and y get new values each time the method calls itself. Its arguments are x*x and y/2. For the x=2 and y=8, the sequence of arguments and return values would be

x=2, y=8 x=4, y=4 x=16, y=2 x=256, y=1 Returning 256, x=256, y=1 Returning 256, x=16, y=2 Returning 256, x=4, y=4 Returning 256, x=2, y=8

When y is 1, we return. The answer, 256, is passed unchanged back up the sequence of methods.

We’ve shown an example in which y is an even number throughout the entire sequence of divisions. This will not usually be the case. Here’s how to revise the algo- rithm to deal with the situation where y is odd. Use integer division on the way down and don’t worry about a remainder when dividing y by 2. However, during the

Some Interesting Recursive Applications 305

return process, whenever y is an odd number, do an additional multiplication by x.

Here’s the sequence for 3 18 :

x=3, y=18 x=9, y=9 x=81, y=4 x=6561, y=2 x=43046721, y=1 Returning 43046721, x=43046721, y=1 Returning 43046721, x=6561, y=2 Returning 43046721, x=81, y=4 Returning 387420489, x=9, y=9 // y is odd; so multiply by x Returning 387420489, x=3, y=18