CSE 20 DISCRETE MATH Prof. Shachar Lovett Clicker frequency: CA
Todays topics Proofs for algorithms Casting out nines Fast exponentiation Section 3.7 in Jenkyns, Stephenson
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?
Casting out nines
Casting out nines: termination
Casting out nines: correctness
Casting out nines: speed
Fast exponentiation
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
Fast exponentiation: correctness Let a*,b* denote original inputs
Fast exponentiation: correctness
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
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
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 Naïve: 1000 multiplications Fast: ~10 multiplications Fast exponentiation is indeed much faster, and hence is used widely in practice
Fast exponentiation: extensions
Next class More proofs for algorithms: Euclid’s algorithm Read section 3.7 in Jenkyns, Stephenson