Raise a number x to the power y

Raise a number x to the power y
Raise a number x to the power y
Example: 6
45
Raise a number x to the power y
Example: 6
45
6*6*6*6*6*...*6
Yikes! 45 multiplies
Raise a number x to the power y
Example: 6
45
6*6*6*6*6*...*6
6 = n[0]
n[0]*n[0] = n[1]
n[1]*n[1] = n[2]
n[2]*n[2] = n[3]
n[3]*n[3] = n[4]
n[4]*n[4] = n[5]
(61)
(62)
(64)
(68)
(616)
(632)
So, 645 = n[5]*n[3]*n[2]*n[0]
Yikes! 45 multiplies
Only 8 multiplies!
Raise a number x to the power y
Example: 6
45
Notice: 6 = 6
45
6 = n[0]
n[0]*n[0] = n[1]
n[1]*n[1] = n[2]
n[2]*n[2] = n[3]
n[3]*n[3] = n[4]
n[4]*n[4] = n[5]
101101
(61)
(62)
(64)
(68)
(616)
(632)
So, 645 = n[5]*n[3]*n[2]*n[0]
(in binary)
Raise a number x to the power y
Example: 6
45
Notice: 6 = 6
45
101101
(in binary)
Also notice: x is the square of
100
x
and
1010100
x
is the square of
101010
x
1000
Raise a number x to the power y
Example: 6
45
Notice: 6 = 6
45
101101
(in binary)
Algorithm:
Find binary equivalent of y.
Define an accumulator variable p and initialize it to x.
Starting with the 2nd most significant bit of y,
repeat the following until all bits of y are considered:
Square p. If the bit is 1, multiply p by x.
Consider the next lesser significant bit of y.
Raise a number x to the power y
Example: 6
45
Raise a number x to the power y
Example: 6
45
So: x = 6, y= 45 = 101101 (in binary)
Raise a number x to the power y
Example: 6
45
So: x = 6, y= 45 = 101101 (in binary)
Set p = 6
Raise a number x to the power y
Example: 6
45
So: x = 6, y= 45 = 101101 (in binary)
*
Set p = 6
nd
2 MSB of y is 0 so square p (p = 36)
Raise a number x to the power y
Example: 6
45
So: x = 6, y= 45 = 101101 (in binary)
*
Set p = 6
nd
2 MSB of y is 0 so square p (p = 36)
Next MSB is 1: square and mult (p = 7776)
Raise a number x to the power y
Example: 6
45
So: x = 6, y= 45 = 101101 (in binary)
*
Set p = 6
nd
2 MSB of y is 0 so square p (p = 36)
Next MSB is 1: square and mult (p = 7776)
Next MSB is 1: square and mult (p = 362797056)
Raise a number x to the power y
Example: 6
45
So: x = 6, y= 45 = 101101 (in binary)
*
Set p = 6
nd
2 MSB of y is 0 so square p (p = 36)
Next MSB is 1: square and mult (p = 7776)
Next MSB is 1: square and mult (p = 362797056)
Next MSB is 0: square (p = 131621703842267136)
Raise a number x to the power y
Example: 6
45
So: x = 6, y= 45 = 101101 (in binary)
*
Set p = 6
nd
2 MSB of y is 0 so square p (p = 36)
Next MSB is 1: square and mult (p = 7776)
Next MSB is 1: square and mult (p = 362797056)
Next MSB is 0: square (p = 131621703842267136)
Last MSB is 1: s&m (p = 103945637534048876111514866313854976)
Raise a number x to the power y
Example: 6
45
How to find the last MSB?
Raise a number x to the power y
Example: 6
45
How to find the last MSB? 101101
*
Raise a number x to the power y
Example: 6
45
How to find the last MSB? 101101
*
(45 % 2)
Raise a number x to the power y
Example: 6
45
How to find the last MSB? 101101
*
(45 % 2)
How to strip off the last MSB?
Raise a number x to the power y
Example: 6
45
How to find the last MSB? 10110 1
*
(45 % 2)
How to strip off the last MSB?
(45 / 2)
Raise a number x to the power y
Example: 6
45
How to find the last MSB?
(45 % 2)
How to strip off the last MSB?
(45 / 2)
How to store all the bits?
Work from LSB to MSB and put the bits
on a stack.