Lecture19BigO - CS2603 Applied Logic for Hardware and...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS2603 Applied Logic for Hardware and Software Rex Page University of Oklahoma 1 Computation Time and the Big O Lecture 19 CS 2603 Applied Logic for Hardware and Software CS2603 Applied Logic for Hardware and Software Rex Page University of Oklahoma 2 Exponentiation the slow way and the fast way Russian peasants algorithm exponentiation, the fast way rp b n = if n == 0 then 1-- rp0 else if (odd n) then b (rp (b b) (n `div` 2))-- rp odd else (rp (b b) (n `div` 2))-- rp even Theorem (RPexp). n N .( b 0.((rp b n) = b n )) Note: Theres a similar algorithm for multiplication Thats the one the peasants were using No need for multiplication tables and almost as fast For a few dollars more, you can do division similarly Proof induction: P(n) ( b 0.((rp b n) = b n )) Base case: n = rp b = 1 rp0 = b 8 th grade algebra 8 `div` 3 = 2 3 rd grade quotient Exponentiation the slow way pow b n = if n == 0 then 1 else b (pow b (n 1)) Theorem: (pow b n) = b n CS2603 Applied Logic for Hardware and Software Rex Page University of Oklahoma 3 Inductive Cases Russian Peasant Proof rp b n = if n == 0 then 1--rp 0 else if (odd n) then b (rp (b b) (n `div` 2))-- rp odd else (rp (b b) (n `div` 2))--rp even P(n) ( b 0.((rp b n) = b n )) Inductive case, odd exponent: n = 2 k + 1 > rp b n = rp b (2 k + 1) n = 2 k + 1 = b (rp (b b) ((2 k + 1) `div` 2)) rp odd = b (rp (b b) k) 3 nd grade arithmetic = b (b b) k P(k) = b (b 2 ) k 8 th grade algebra = b 2k+1 8 th grade algebra = b n n = 2 k + 1 Inductive case, even exponent: n = 2 k > similar qed CS2603 Applied Logic for Hardware and Software Rex Page University of Oklahoma 4 Fast Peasants Russian peasants algorithm for exponentiation rp b n = if n == 0 then 1-- rp0} else if (odd n) then b (rp (b b) (n `div` 2))-- rp odd else (rp (b b) (n `div` 2))-- rp even Theorem (RPexp). n N .( b 0.((rp b n) = b n )) How long does it take rp to deliver?...
View Full Document

Page1 / 15

Lecture19BigO - CS2603 Applied Logic for Hardware and...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online