Download presentation
Presentation is loading. Please wait.
Published byAmberlynn Hunter Modified over 9 years ago
1
Problem 1.b Theorem 1: For all n≥0, the function call fac n evaluates to an answer n!: let rec fac n = if n <= 0 then 1 else n * fac (n-1) 0! = 1 n! = n (n-1)! Proof: by induction on natural number n. (Basis) n = 0, fac 0 = 1 by the definition of fac = 0! by the definition of ! (Induction hypothesis) For a natural number k ≥ 0, fac k evaluates to k!. (Induction step) It suffices to show that fac (k+1) evaluates to (k+1)! fac (k+1) = (k+1) fac k by the definition of fac = (k+1) k! by induction hypothesis = (k+1)! by the definition of ! Hanyang University - ERICAComputer Science & Engineering CSE215 Fundamentals of Program Design Homework #x Fall 2009 2008xxxxxx 홍길동
2
Problem 1.b Theorem 2 : For all n≥0, the function call fact n evaluates to an answer n!: let fact n = let rec loop n p = if n <= 0 then p else loop (n-1) (n*p) in loop n 1 0! = 1 n! = n (n-1)! Proof: We prove by induction on n that for all n≥0, loop n p evaluates to p n!. (Basis) n = 0, loop 0 p = p by the definition of loop = p 0! by the definition of ! (Induction hypothesis) For a natural number k ≥ 0, loop k p evaluates to p k!. (Induction step) It suffices to show that loop (k+1) p evaluates to p (k+1)! loop (k+1) p = loop k (k+1) p by the definition of loop = (k+1) p k! by the induction hypothesis = p (k+1)! by the definition of ! Then fact n = loop n 1 = 1 n! = n! Hanyang University - ERICAComputer Science & Engineering CSE215 Fundamentals of Program Design Homework #x Fall 2009 2008xxxxxx 홍길동
3
Problem 1.b Proof: by induction on n. (Basis) n = 0, exp b 0 = 1. by the definition of exp = b 0 (Induction hypothesis) For an arbitrary integer k ≥ 0, exp b k evaluates to b k. (Induction step) It suffices to show that exp b (k+1) evaluates to b (k+1) exp b (k+1) = b exp b k by the definition of exp = b b k by induction hypothesis = b (k+1) Theorem 3 : For all integer n≥0, the function call exp b n calculates b n for some floating-point number b. let rec exp b n = if n=0 then 1. else b *. exp b (n-1) Hanyang University - ERICAComputer Science & Engineering CSE215 Fundamentals of Program Design Homework #x Fall 2009 2008xxxxxx 홍길동
4
Problem 1.b Proof: We prove by induction on n that for all n≥0, loop n r evaluates to r b n (Basis) n = 0, loop 0 r = r by the definition of loop = r b 0 (Induction hypothesis) For a natural number k ≥ 0, loop k r evaluates to r b k (Induction step) It suffices to show that loop (k+1) r evaluates to r b k+1 loop (k+1) r = loop k (b r) by the definition of loop = (b r) b k by the induction hypothesis = r b b k = r b k+1 Then expt b n = loop n 1 = 1 b n = b n let expt b n = let rec loop n r = if n=0 then r else loop (n-1) (b*.r) in loop n 1. Theorem 4 : For all integer n≥0, the function call expt b n calculates b n for some floating-point number b. Hanyang University - ERICAComputer Science & Engineering CSE215 Fundamentals of Program Design Homework #x Fall 2009 2008xxxxxx 홍길동
5
Problem 1.b Proof: by strong induction on n. (Basis) n = 0, fast_exp b 0 = 1. by the definition of exp = b 0 (Induction hypothesis) For every natural number k n, fast_exp b k evaluates to b k. (Induction step) It suffices to show that fast_exp b (n+1) evaluates to b (n+1) case 1: n+1 is odd, fast_exp b (n+1) = b fast_exp b n by the definition of exp = b b n by induction hypothesis = b n+1 case 2: n+1 is even, fast_exp b (n+1) = (fast_exp b (n+1/2))**2 = (b (n+1)/2 ) 2 = b n+1 let rec fast_exp b n = if n=0 then 1. else if (n mod 2 = 0) then (fast_exp b (n/2))**2. else b *. fast_exp b (n-1) Theorem 5 : For all integer n≥0, the function call fast_exp b n calculates b n for some floating-point number b. Hanyang University - ERICAComputer Science & Engineering CSE215 Fundamentals of Program Design Homework #x Fall 2009 2008xxxxxx 홍길동
6
Problem 1.b Proof: by strong induction on n that for all n≥0, loop b n r evaluates to r b n (Basis) n = 0, loop b 0 r = r by the definition of loop = r 1 = r b 0 (Induction hypothesis) For every natural number k n, loop b k r evaluates to r b k. (Induction step) It suffices to show that loop b (n+1) r evaluates to r b (n+1) case 1: n+1 is odd, loop b (n+1) r = loop b n (b*.r) by the definition of loop = (b r) b n by induction hypothesis = r b n+1 case 2: n+1 is even, loop b (n+1) r = loop (b**2.) ((n+1)/2) r = r (b 2 ) (n+1)/2 = r b n+1 let fast_expt b n = let rec loop b n r = if n=0 then r else if (n mod 2=0) then loop (b**2.) (n/2) r else loop b (n-1) (b*.r) in loop b n 1. Theorem 6 : For all integer n≥0, the function call fast_expt b n calculates b n for some floating-point number b : CSE215 Fundamentals of Program Design Homework #x Fall 2009 2008xxxxxx 홍길동 Hanyang University - ERICAComputer Science & Engineering
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.