Problem 1.b Theorem 1: For all n≥0, the function call fac n evaluates to an answer n!: let rec fac...

6
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 Hanyang University - ERICA Computer Science & Engineering CSE215 Fundamentals of Program Design Homework #x Fall 2009 2008xxxxxx 홍홍홍

Transcript of Problem 1.b Theorem 1: For all n≥0, the function call fac n evaluates to an answer n!: let rec fac...

Page 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

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! = 1n! = 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 - ERICA Computer Science & Engineering

CSE215 Fundamentals of Program DesignHomework #x

Fall 20092008xxxxxx 홍길동

Page 2: 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

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! = 1n! = n (n-1)!

Proof: We prove by induction on n that for all n≥0, loop n p evaluates to pn!. (Basis) n = 0, loop 0 p = p by the definition of loop = p0! by the definition of ! (Induction hypothesis) For a natural number k ≥ 0, loop k p evaluates to pk!. (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)pk! by the induction hypothesis = p(k+1)! by the definition of !Then fact n = loop n 1 = 1n! = n!

Hanyang University - ERICA Computer Science & Engineering

CSE215 Fundamentals of Program DesignHomework #x

Fall 20092008xxxxxx 홍길동

Page 3: 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

Problem 1.b

Proof: by induction on n.(Basis) n = 0, exp b 0 = 1. by the definition of exp

= b0

(Induction hypothesis) For an arbitrary integer k ≥ 0, exp b k evaluates to bk.

(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 bk by induction hypothesis

= b(k+1)

Theorem 3 : For all integer n≥0, the function call exp b n calculates bn for some floating-point number b.

let rec exp b n = if n=0 then 1. else b *. exp b (n-1)

Hanyang University - ERICA Computer Science & Engineering

CSE215 Fundamentals of Program DesignHomework #x

Fall 20092008xxxxxx 홍길동

Page 4: 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

Problem 1.b

Proof: We prove by induction on n that for all n≥0, loop n r evaluates to rbn

(Basis) n = 0, loop 0 r = r by the definition of loop = rb0 (Induction hypothesis) For a natural number k ≥ 0, loop k r evaluates to r bk

(Induction step) It suffices to show that loop (k+1) r evaluates to rbk+1

loop (k+1) r = loop k (br) by the definition of loop = (br) bk by the induction hypothesis = rb bk = rbk+1 Then expt b n = loop n 1 = 1bn = bn

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 bn for some floating-point number b.

Hanyang University - ERICA Computer Science & Engineering

CSE215 Fundamentals of Program DesignHomework #x

Fall 20092008xxxxxx 홍길동

Page 5: 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

Problem 1.b

Proof: by strong induction on n.(Basis) n = 0, fast_exp b 0 = 1. by the definition of exp = b0

(Induction hypothesis) For every natural number k n, fast_exp b k evaluates to bk.(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 bn by induction hypothesis = bn+1

case 2: n+1 is even, fast_exp b (n+1) = (fast_exp b (n+1/2))**2 = (b(n+1)/2)2 = bn+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 bn for some floating-point number b.

Hanyang University - ERICA Computer Science & Engineering

CSE215 Fundamentals of Program DesignHomework #x

Fall 20092008xxxxxx 홍길동

Page 6: 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

Problem 1.b

Proof: by strong induction on n that for all n≥0, loop b n r evaluates to rbn

(Basis) n = 0, loop b 0 r = r by the definition of loop = r 1 = r b0

(Induction hypothesis) For every natural number k n, loop b k r evaluates to r bk.(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

= (br) bn by induction hypothesis = r bn+1

case 2: n+1 is even, loop b (n+1) r = loop (b**2.) ((n+1)/2) r = r (b2)(n+1)/2 = r bn+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 bn for some floating-point number b:

CSE215 Fundamentals of Program DesignHomework #x

Fall 20092008xxxxxx 홍길동

Hanyang University - ERICA Computer Science & Engineering