Mathematical Induction - COM1022 Functional

Mathematical Induction
COM1022 Functional Programming Techniques
Professor Steve Schneider
University of Surrey
Semester 2, 2010 – Week 09
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
1 / 13
Outline
1
Proving statements about functions
2
The principle of induction
3
Examples
4
Take and drop
5
Exercises (in place of lab)
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
2 / 13
Proving statements about functions
Proving statements about functions
oddsum 0 = 0
oddsum n = oddsum (n-1) + (2*n - 1)
oddsum 0 = ?
oddsum 1 = ?
oddsum 2 = ?
...
Can we spot a pattern?
For all n: oddsum n = ?
Can we prove it?
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
3 / 13
Proving statements about functions
The general form
Let P(n) be a statement (predicate) about n.
e.g. P(n) ≡ ”oddsum n = n2 ”
∀n.P(n) asserts that P(n) is true for all n.
How can we prove this? We can’t check each n in turn, because
there are infinitely many of them.
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
4 / 13
The principle of induction
The principle of induction
If P(0)
and P(n) ⇒ P(n + 1) for all n
Then we can conclude ∀n.P(n)
An infinite sequence of dominoes...
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
5 / 13
The principle of induction
Base case: P(0)
P(0) is true:
oddsum 0 = 02
The initial case, that is proved directly, is called the base case.
It often corresponds to the base case of the functional definition.
It is typically established by examining the statement P(0) directly.
[The base case can be some number other than 0. The result will
hold for all numbers from the base case.]
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
6 / 13
The principle of induction
Inductive step: P(n) ⇒ P(n + 1)
We have to show, for any n, that if P(n) is true, then P(n + 1) is
also true.
So assume, for some arbitrary n, that P(n) is true. Let’s see if we
can prove that P(n + 1) is true.
oddsum (n + 1) = oddsum n + (2n + 1)
= n2
= (n + 1)
+ (2n + 1)
2
So if P(n) is true, then P(n + 1) is also true
Hence by induction, ∀n.P(n)
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
7 / 13
Examples
Example: 4 divides (5n − 1)
We can prove that 5n − 1 is always divisible by 4.
Base case: n = 0
Inductive step: assume true for n, prove for n + 1
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
8 / 13
Examples
Example: sigma n
sigma 0 = 0
sigma n = n + sigma (n-1)
Q(n) : sigma n =
n(n+1)
2
Base case: show Q(0)
Inductive step: show Q(n) ⇒ Q(n + 1)
Then conclude ∀n.Q(n)
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
9 / 13
Take and drop
take and drop
take 0 xs = []
take n [] = []
take n (x:xs) = x:(take (n-1) xs)
drop 0 xs = xs
drop n [] = []
drop n (x:xs) = drop (n-1) xs
Is it true that (take n xs) ++ (drop n xs) = xs?
What if n is greater than the length of xs?
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
10 / 13
Take and drop
a property of take and drop
Want to prove that take n xs ++ drop n xs = xs for all n.
We can prove this by induction:
Base case: (take 0 xs) ++ (drop 0 xs) = [] ++ xs = xs
Inductive step: need to consider [] and x:xs:
(take (n+1) []) ++ (drop (n+1) []) = [] ++ [] = []
take (n+1) (x:xs) ++ drop (n+1) (x:xs)
= x:(take n xs) ++ (drop n xs)
= x:((take n xs) ++ (drop n xs))
= x:(xs)
= x:xs
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
11 / 13
Take and drop
Summary
The principle of (mathematical) induction
To prove some property P(n) is true for all n:
Show the base case: P(0)
Show the inductive step: P(n) ⇒ P(n + 1)
Professor Steve Schneider
Mathematical Induction
Semester 2, 2010 – Week 09
12 / 13
Exercises (in place of lab)
Exercises
These exercises are instead of a lab for this week. Please try them in
advance of next week.
Prove each of the following by induction:
20 + 21 + ... + 2n = 2(n+1) − 1
12 + 22 + ... + n2 =
(n(n+1)(2n+1))
6
4n + 6n − 1 is divisible by 9
Professor Steve Schneider
for all n
for all n
for all n
Mathematical Induction
Semester 2, 2010 – Week 09
13 / 13