Raise a number x to the power y
Raise a number x to the power y Example: 645
Raise a number x to the power y Example: 645 6*6*6*6*6*...*6 Yikes! 45 multiplies
Raise a number x to the power y Example: 645 6*6*6*6*6*...*6 Yikes! 45 multiplies 6 = n[0] (61) n[0]*n[0] = n[1] (62) n[1]*n[1] = n[2] (64) n[2]*n[2] = n[3] (68) n[3]*n[3] = n[4] (616) n[4]*n[4] = n[5] (632) So, 645 = n[5]*n[3]*n[2]*n[0] Only 8 multiplies!
Raise a number x to the power y Example: 645 Notice: 645 = 6101101 (in binary) 6 = n[0] (61) n[0]*n[0] = n[1] (62) n[1]*n[1] = n[2] (64) n[2]*n[2] = n[3] (68) n[3]*n[3] = n[4] (616) n[4]*n[4] = n[5] (632) So, 645 = n[5]*n[3]*n[2]*n[0]
Raise a number x to the power y Example: 645 Notice: 645 = 6101101 (in binary) Also notice: x1000 is the square of x100 and x1010100 is the square of x101010
Raise a number x to the power y Example: 645 Notice: 645 = 6101101 (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: 645
Raise a number x to the power y Example: 645 So: x = 6, y= 45 = 101101 (in binary)
Raise a number x to the power y Example: 645 So: x = 6, y= 45 = 101101 (in binary) Set p = 6
Raise a number x to the power y Example: 645 So: x = 6, y= 45 = 101101 (in binary) * Set p = 6 2nd MSB of y is 0 so square p (p = 36)
Raise a number x to the power y Example: 645 So: x = 6, y= 45 = 101101 (in binary) * Set p = 6 2nd 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: 645 So: x = 6, y= 45 = 101101 (in binary) * Set p = 6 2nd 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: 645 So: x = 6, y= 45 = 101101 (in binary) * Set p = 6 2nd 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: 645 So: x = 6, y= 45 = 101101 (in binary) * Set p = 6 2nd 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: 645 How to find the last MSB?
Raise a number x to the power y Example: 645 How to find the last MSB? 101101 *
Raise a number x to the power y Example: 645 How to find the last MSB? 101101 * (45 % 2)
Raise a number x to the power y Example: 645 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: 645 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: 645 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.