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! =

Slides:



Advertisements
Similar presentations
Extensible Networking Platform CSE 240 – Logic and Discrete Mathematics Review: Mathematical Induction Use induction to prove that the sum of the.
Advertisements

Mathematical Induction
Review for CS1050. Review Questions Without using truth tables, prove that  (p  q)   q is a tautology. Prove that the sum of an even integer and an.
Mathematical induction Isaac Fung. Announcement ► Homework 1 released ► Due on 6 Oct 2008 (in class)
1 Mathematical Induction. 2 Mathematical Induction: Example  Show that any postage of ≥ 8¢ can be obtained using 3¢ and 5¢ stamps.  First check for.
1 Mathematical Induction. 2 Mathematical Induction: Example  Show that any postage of ≥ 8¢ can be obtained using 3¢ and 5¢ stamps.  First check for.
What is the best way to start? 1.Plug in n = 1. 2.Factor 6n 2 + 5n Let n be an integer. 4.Let n be an odd integer. 5.Let 6n 2 + 5n + 4 be an odd.
Discrete Structures Chapter 5: Sequences, Mathematical Induction, and Recursion 5.2 Mathematical Induction I [Mathematical induction is] the standard proof.
CS2420: Lecture 2 Vladimir Kulyukin Computer Science Department Utah State University.
1 Homework #1 Solutions 2 #1. True or False a)Given a language (set of strings) L, the question: “Is string w  L” is a decision problem: T F b)  =
1 Strong Mathematical Induction. Principle of Strong Mathematical Induction Let P(n) be a predicate defined for integers n; a and b be fixed integers.
Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language.
1 Mathematical Induction. 2 Mathematical Induction: Example  Show that any postage of ≥ 8¢ can be obtained using 3¢ and 5¢ stamps.  First check for.
Mathematical Maxims and Minims, 1988
Mathematical Induction. F(1) = 1; F(n+1) = F(n) + (2n+1) for n≥ F(n) n F(n) =n 2 for all n ≥ 1 Prove it!
Copyright © Peter Cappello Mathematical Induction Goals Explain & illustrate construction of proofs of a variety of theorems using mathematical induction.
CSE 311 Foundations of Computing I Lecture 15 Recursive Definitions and Structural Induction Autumn 2011 CSE 3111.
INDUCTION AND RECURSION. PRINCIPLE OF MATHEMATICAL INDUCTION To prove that P(n) is true for all positive integers n, where P(n) is a propositional function,
1 CMSC 250 Chapter 4, con't., Inductive Proofs. 2 CMSC 250 Description l Inductive proofs must have: –Base case: where you prove that what it is you are.
Mathematical Induction
13-1 Coloring Regions with Two Colors. Theorem The regions formed by n circles in the plane can be colored with red and blue in such a way that.
Cpt S 223 – Advanced Data Structures Math Review 2
Section 3.3: Mathematical Induction Mathematical induction is a proof technique that can be used to prove theorems of the form:  n  Z +,P(n) We have.
Methods of Proof Dr. Yasir Ali. Proof A (logical) proof of a statement is a finite sequence of statements (called the steps of the proof) leading from.
October 3, 2001CSE 373, Autumn Mathematical Background Exponents X A X B = X A+B X A / X B = X A-B (X A ) B = X AB X N +X N = 2X N 2 N +2 N = 2 N+1.
Kyung-Goo Doh Hanyang University - ERICAComputer Science & Engineering Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design.
Inductive Proofs and Inductive Definitions Jim Skon.
Copyright © Zeph Grunschlag, Induction Zeph Grunschlag.
1 CMSC 341 Math Review. 2 Exponents Identities (X A ) B = X AB X A * X B = X A+B X A / X B = X A-B X A + X B  X A+B.
Mathematical Induction 1. 2 Suppose we have a sequence of propositions which we would like to prove: P (0), P (1), P (2), P (3), P (4), … P (n), … We.
1 Discrete Mathematical Mathematical Induction ( الاستقراء الرياضي )
Mathematical Induction
CSE 311 Foundations of Computing I Lecture 15 Strong Induction and Recursive Definitions Spring
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
Copyright © Zeph Grunschlag, Induction Zeph Grunschlag.
1 Mathematical Induction. 2 What is induction? A method of proof It does not generate answers: it only can prove them Three parts: Base case(s): show.
1 Mathematical Induction An inductive proof has three parts: –Basis case –Inductive hypothesis –Inductive step Related to recursive programming.
1 Proofs by Counterexample & Contradiction There are several ways to prove a theorem:  Counterexample: By providing an example of in which the theorem.
Direct Proof and Counterexample IV: Division into Cases and the Quotient-Remainder Theorem For each of the following values of n and d, find integers q.
Advanced Algorithms Analysis and Design
CSE 311 Foundations of Computing I
CSE 311 Foundations of Computing I
Induction and recursion
CSE 311: Foundations of Computing
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Use mathematical induction to prove that the formula is true for all natural numbers m. {image} Choose the first step of the proof from the following:
Mathematical Induction Recursion
CSE 311: Foundations of Computing
BaSIC Math Reviews.
The sum of any two even integers is even.
CSE 311: Foundations of Computing
Fundamental Theorem of Algebra
CSE 321 Discrete Structures
CSE 373 Data Structures and Algorithms
The Fundamental Theorem of Calculus
Applied Discrete Mathematics Week 9: Integer Properties
Induction (Section 3.3).
CSE 373 Optional Section Led by Yuanwei, Luyi Apr
Follow me for a walk through...
Induction Chapter
Advanced Analysis of Algorithms
Mathematical Induction
Induction Rosen 5 Lecture 8: Oct 29, 30.
Follow me for a walk through...
Depth of an AVL tree Theorem: Any AVL tree with n nodes has height less than log n. Proof: Given an n-node AVL tree, we want to find an upper bound.
CSE 321 Discrete Structures
CS 403: Programming Languages
Useful GCD Fact If a and b are positive integers, then gcd(a,b) = gcd(b, a mod b) Proof: By definition of mod, a = qb+ (a mod b) for.
CSE 311 Foundations of Computing I
Recursion.
Presentation transcript:

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 xxxxxx 홍길동

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 xxxxxx 홍길동

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 xxxxxx 홍길동

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 xxxxxx 홍길동

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 xxxxxx 홍길동

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 xxxxxx 홍길동 Hanyang University - ERICAComputer Science & Engineering