Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 20 DISCRETE MATH Prof. Shachar Lovett Clicker frequency: CA.

Similar presentations


Presentation on theme: "CSE 20 DISCRETE MATH Prof. Shachar Lovett Clicker frequency: CA."— Presentation transcript:

1 CSE 20 DISCRETE MATH Prof. Shachar Lovett http://cseweb.ucsd.edu/classes/wi15/cse20-a/ Clicker frequency: CA

2 Todays topics Proofs for algorithms Casting out nines Fast exponentiation Section 3.7 in Jenkyns, Stephenson

3 Casting out nines Goal: check if a number n is divisible by 9 Algorithm: Keep summing the digits (in base 10) Until we get a 1-digit number Which is easy to check Questions: Does the algorithm terminate? Does it return the correct answer? How fast?

4 Casting out nines

5

6 Casting out nines: termination

7

8 Casting out nines: correctness

9

10

11

12 Casting out nines: speed

13

14

15 Fast exponentiation

16 Questions: Does the algorithm terminate? Does it return the correct answer? How fast? Termination is simple: dividing b by 2 at each step (and rounding down), guarantees that eventually b=0 We will analyze correctness and speed

17 Fast exponentiation: correctness Let a*,b* denote original inputs

18 Fast exponentiation: correctness

19 Fast exponentiation: speed Question 3: how fast is the algorithm? Each iteration takes constant time How many iterations? A. a B. b C. log(b) D. log*(b) E. Other

20 Fast exponentiation: speed Question 3: how fast is the algorithm? Each iteration takes constant time How many iterations? A. a B. b C. log(b) D. log*(b) E. Other

21 Fast exponentiation: speed Algorithm to compute a b requires ~log(b) operations Compare to naïve algorithm, which multiplies a with itself b times, and requires b operations Example: compute 2 1000 Naïve: 1000 multiplications Fast: ~10 multiplications Fast exponentiation is indeed much faster, and hence is used widely in practice

22 Fast exponentiation: extensions

23 Next class More proofs for algorithms: Euclid’s algorithm Read section 3.7 in Jenkyns, Stephenson


Download ppt "CSE 20 DISCRETE MATH Prof. Shachar Lovett Clicker frequency: CA."

Similar presentations


Ads by Google