Presentation is loading. Please wait.

Presentation is loading. Please wait.

Raise a number x to the power y

Similar presentations


Presentation on theme: "Raise a number x to the power y"— Presentation transcript:

1 Raise a number x to the power y

2 Raise a number x to the power y
Example: 645

3 Raise a number x to the power y
Example: 645 6*6*6*6*6*...* Yikes! 45 multiplies

4 Raise a number x to the power y
Example: 645 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!

5 Raise a number x to the power y
Example: 645 Notice: = (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]

6 Raise a number x to the power y
Example: 645 Notice: = (in binary) Also notice: x1000 is the square of x100 and x is the square of x101010

7 Raise a number x to the power y
Example: 645 Notice: = (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.

8 Raise a number x to the power y
Example: 645

9 Raise a number x to the power y
Example: 645 So: x = 6, y= 45 = (in binary)

10 Raise a number x to the power y
Example: 645 So: x = 6, y= 45 = (in binary) Set p = 6

11 Raise a number x to the power y
Example: 645 So: x = 6, y= 45 = (in binary) * Set p = 6 2nd MSB of y is 0 so square p (p = 36)

12 Raise a number x to the power y
Example: 645 So: x = 6, y= 45 = (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)

13 Raise a number x to the power y
Example: 645 So: x = 6, y= 45 = (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 = )

14 Raise a number x to the power y
Example: 645 So: x = 6, y= 45 = (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 = ) Next MSB is 0: square (p = )

15 Raise a number x to the power y
Example: 645 So: x = 6, y= 45 = (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 = ) Next MSB is 0: square (p = ) Last MSB is 1: s&m (p = )

16 Raise a number x to the power y
Example: 645 How to find the last MSB?

17 Raise a number x to the power y
Example: 645 How to find the last MSB? *

18 Raise a number x to the power y
Example: 645 How to find the last MSB? * (45 % 2)

19 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?

20 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)

21 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.


Download ppt "Raise a number x to the power y"

Similar presentations


Ads by Google