CSE 20 DISCRETE MATH Prof. Shachar Lovett http://cseweb.ucsd.edu/classes/wi15/cse20-a/ 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 CastingOutNines(n): Input: integer 𝑛 ≥ 0 Output: is n divisible by 9 1. While 𝑛 ≥ 10: a. Express n in base 10: 𝑛 = 𝑏𝑘 𝑏𝑘−1 … 𝑏0 b. Set n ← 𝑏𝑘 + 𝑏𝑘−1 + ⋯ + 𝑏0 2. Return TRUE if n=0 or n=9, FALSE otherwise Casting out nines • Question 1: does it always terminate? • Let SUM(n)=“sum of digits of n in base 10” • To prove that the algorithm always terminates, we need to prove that whenever 𝑛 ≥ 10, we have SUM(n) < n. • This will show that the following loop must terminate: While 𝑛 ≥ 10: 𝑛 ← 𝑆𝑈𝑀(𝑛) Casting out nines: termination • SUM(n)=“sum of digits of n in base 10” • Lemma: if 𝑛 ≥ 10 then 𝑆𝑈𝑀 𝑛 < 𝑛 • What do you think is the best way to prove this? A. Direct proof B. Proof by contraposition C. Proof by contradiction D. Proof by induction E. Proof by strong induction Casting out nines: termination • SUM(n)=“sum of digits of n in base 10” • Lemma: if 𝑛 ≥ 10 then 𝑆𝑈𝑀 𝑛 < 𝑛 • Proof (direct proof): Say 𝑛 = 𝑏𝑘 𝑏𝑘−1 … 𝑏0 is the base-10 representation of n. Assumption 𝑛 ≥ 10 means at least 2-digits, ie 𝑘 ≥ 1 Then: • 𝑛 = 10𝑘 𝑏𝑘 + 10𝑘−1 𝑏𝑘−1 + ⋯ + 10𝑏1 + 𝑏0 • 𝑆𝑈𝑀(𝑛) = 𝑏𝑘 + 𝑏𝑘−1 + ⋯ + 𝑏1 + 𝑏0 So: 𝑘 𝑘 10𝑖 − 1 𝑏𝑖 ≥ 𝑛 − 𝑆𝑈𝑀 𝑛 = 𝑖=0 𝑏𝑖 ≥ 1 𝑖=1 Casting out nines: correctness • Question 2: does it return the correct answer? • SUM(n)=“sum of digits of n in base 10” • Lemma: n is divisible by 9 ⇔ SUM(n) is divisible by 9 Casting out nines: correctness • SUM(n)=“sum of digits of n in base 10” • Lemma: n is divisible by 9 ⇔ SUM(n) is divisible by 9 • Proof idea: show that n-SUM(n) is always divisible by 9 • We saw that if 𝑛 = 𝑏𝑘 𝑏𝑘−1 … 𝑏0 in base 10 then 𝑘 10𝑖 − 1 𝑏𝑖 𝑛 − 𝑆𝑈𝑀 𝑛 = 𝑖=0 • Sufficient to prove: ∀𝑖 ≥ 0, 9 | 10𝑖 − 1 Casting out nines: correctness • Claim: ∀𝑖 ≥ 0, 9 | 10𝑖 − 1 • What do you think is the best way to prove this? A. Direct proof B. Proof by contraposition C. Proof by contradiction D. Proof by induction E. Proof by strong induction Casting out nines: correctness Claim: ∀𝑖 ≥ 0, 9 | 10𝑖 − 1 Proof (by induction): Base case: i=0. 100 − 1 = 0 divisible by 9. Inductive case: Assume: 10𝑖−1 − 1 is divisible by 9 WTS: 10𝑖 − 1 is divisible by 9 We have: 10𝑖 − 1 = 10 10𝑖−1 − 1 + 9 By inductive assumption, ∃𝑘 ∈ 𝑍, 10𝑖−1 − 1 = 9𝑘. So 10𝑖 − 1 = 10 ⋅ 9𝑘 + 9 = 9(10𝑘 + 1), hence 10𝑖 − 1 is divisible by 9. QED Casting out nines: speed • Question 3: how fast is the algorithm? • Really, the question is: how much smaller is SUM(n) compared to n? • This will determine how fast n gets to be single-digit • Say 10𝑘 ≤ 𝑛 < 10𝑘+1 • Then 𝑛 = 𝑏𝑘 𝑏𝑘−1 … 𝑏0 has k+1 digits • Hence 𝑆𝑈𝑀 𝑛 ≤ 9(𝑘 + 1) • So 𝑆𝑈𝑀(𝑛) is at most ≈ 9 ⋅ log10 𝑛 Casting out nines: speed • In each iteration, replace n with 𝑆𝑈𝑀 𝑛 ≈ log 𝑛 • How many iterations do we need, until n is a single digit number? A. n 𝑛 C. log n D. log log n E. Other B. Casting out nines: speed • Answer: ~log*(n) operations • Definition of the log* function: number of logs required to get a number to be below 1 • Example (with base-10 logarithm): • log ∗ 10 = 1 • log ∗ 1010 = 2 • log … ∗ 1010 10 =3 Fast exponentiation FastExponent(a,b) Inputs: 𝑎 ∈ 𝑅, 𝑏 ∈ 𝑁 Output: 𝑎𝑏 1. res=1 2. While 𝑏 ≥ 1: a. If (b mod 2==1): res ← res * a b. b ← b div 2 c. a ← a2 3. Return res 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 FastExponent(a,b) Inputs: 𝑎 ∈ 𝑅, 𝑏 ∈ 𝑁 Output: 𝑎𝑏 Let a*,b* denote original inputs 1. res=1 2. While 𝑏 ≥ 1: a. If (b mod 2==1): res ← res * a b. b ← b div 2 c. a ← a2 3. Return res Loop invariant: ∗ 𝑟𝑒𝑠 ∗ 𝑎𝑏 = 𝑎∗ 𝑏 res=1 While 𝑏 ≥ 1: a. If (b mod 2==1): res ← res ∗ a b. b ← b div 2 c. a ← a2 Fast exponentiation: correctness • Lemma: every time at the beginning of the loop, we have ∗ ∗ 𝑏 𝑎 𝑎𝑏 • • • • 𝑟𝑒𝑠 ∗ = Proof (by induction): Let 𝑎𝑖 , 𝑏𝑖 , 𝑟𝑒𝑠𝑖 be the values at the beginning of i-th loop Base case (i=0): 𝑎0 = 𝑎∗ , 𝑏0 = 𝑏 ∗ , 𝑟𝑒𝑠0 = 1 Inductive case: break to cases, whether bi is even or odd • If 𝑏𝑖 = 2𝑘 then a𝑖+1 = 𝑎𝑖2 , 𝑏𝑖+1 = 𝑘; 𝑟𝑒𝑠𝑖+1 = 𝑟𝑒𝑠𝑖 and 𝑟𝑒𝑠𝑖+1 ∗ 𝑎𝑖+1 𝑏𝑖+1 = 𝑟𝑒𝑠𝑖 ∗ 2 𝑘 𝑎𝑖 = 𝑟𝑒𝑠𝑖 ∗ 𝑎𝑖 2𝑘 = 𝑟𝑒𝑠𝑖 ∗ 𝑎𝑖 𝑏𝑖 • If 𝑏𝑖 = 2𝑘 + 1 then a𝑖+1 = 𝑎𝑖2 , 𝑏𝑖+1 = 𝑘; 𝑟𝑒𝑠𝑖+1 = 𝑟𝑒𝑠𝑖 ∗ 𝑎 and 𝑟𝑒𝑠𝑖+1 ∗ 𝑎𝑖+1 𝑏𝑖+1 = 𝑟𝑒𝑠𝑖 ∗ 𝑎 ∗ 𝑎𝑖2 𝑘 = 𝑟𝑒𝑠𝑖 ∗ 𝑎𝑖 2𝑘+1 = 𝑟𝑒𝑠𝑖 ∗ 𝑎𝑖 𝑏𝑖 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 res=1 While 𝑏 ≥ 1: a. If (b mod 2==1): res ← res ∗ a b. b ← b div 2 c. a ← a2 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 res=1 While 𝑏 ≥ 1: a. If (b mod 2==1): res ← res ∗ a b. b ← b div 2 c. a ← a2 Fast exponentiation: speed • Algorithm to compute ab requires ~log(b) operations • Compare to naïve algorithm, which multiplies a with itself b times, and requires b operations • Example: compute 21000 • Naïve: 1000 multiplications • Fast: ~10 multiplications • Fast exponentiation is indeed much faster, and hence is used widely in practice Fast exponentiation: extensions • We analyze the algorithm for the case of 𝑎 ∈ 𝑅 • However, it works in any domain, as long as there is a multiplication operator defined • For example, raising a matrix to a high power (which is one of the components in Google’s PageRank) Next class • More proofs for algorithms: Euclid’s algorithm • Read section 3.7 in Jenkyns, Stephenson