A sequence is defined recursively if the first k terms are given explicitly (the initial
conditions), and then the rest of the terms are defined by a recursive formula.
-4&8
Induction & Recursion P. Danziger
1
Sequences & Series (4.1)
1.1
Sequences
A Sequence is an ordered set of numbers. For example the sequence of even numbers, 2, 4, 6, 8, 10, . . . We usually write ai , i = 1 . . . n to denote a sequence with n terms. ai , i = 1, 2 . . . if the sequence has an infinite number of terms. In general there are two ways to define the elements of a sequence: 1. A sequence is defined analytically if each term ai is given by some known formula of i. i.e. ai = f (i). 2. A sequence is defined recursively if the first k terms are given explicitly (the initial conditions), and then the rest of the terms are defined by a recursive formula. i.e. a1 , . . . ak are given and an = f (an−1 , . . . , ai−k ) for n > k. Example 1 1. The sequence of even numbers can be defined analytically by ai = 2i. 2. The recursive definition: a1 = 2, an = 2an−1 for n > 1, is the same as the analytic definition: an = 2n 1 = 1, 1 , 1 , 1 , 1 , . . . 3. an = n 2 3 4 5 4. The Fibonnacci Sequence: a1 = a2 = 1, ak = ak−1 + ak−2 , k > 2, 1, 1, 2, 3, 5, 8, 13, . . .. Sequences arise naturally in the course of for and while loops, particularly when dealing with arrays. for(i = 0; i < n; i++) { a=2 a[i] = 2i; while(a < n) { printf(”%d”, a[i]); printf(”%d”, a); } a = 2a; } The for loop prints out the first n even numbers (analytic). The while loop prints out all powers of two less than n (recursive). A common question is: Given a sequence defined recursively, find an analytic representation of the same sequence (or vice versa).
1
-4&8
1.2
Induction & Recursion
P. Danziger
Series
Definition 2 A series is the sum of all the terms in a sequence. Given a sequence ai and two positive integers m and n, with n ≥ m n X
ai = am + am+1 + am+2 + . . . + an
i=m
P We use the summation symbol to indicate the sum of a number of terms, the lower index indicates the start of the P sum, and the upper index the end. If a sum consists of an unbounded number of terms we write ∞ i=m ai = am + am+1 + am+2 + . . . Example 3 1.
P5
i = 1 + 2 + 3 + 4 + 5 = 15.
2.
P6
i = 3 + 4 + 5 + 6 = 18.
3.
P5
4.
P3
1.2.1
i=1
i=3
i i=1 (−1)
k=1
= (−1) + (−1)2 + (−1)3 + (−1)4 + (−1)5 = −1 + 1 − 1 + 1 + −1 = −1.
1 = 1 + 1 + 1 = 11 . 2 3 6 k
Change of variable
P P Let ai be a sequence, consider the series ni=1 ai and n+1 i=2 ai−1 . Pn ai = a1 + a2 + . . . + an Pi=1 n+1 i=2 ai−1 = a1 + a2 + . . . + an So these are the same series. P In general, given a series ni=m ai , we may change the variable by setting j = i + k (k ∈ Z) to P obtain a series in a different form, n+k j=m+k aj−k . Note that we must change the limits, as well as the summand. Example 4 Pn 2 i=1 (i + 1) . LetP j = i + 1, then P when i = 1, j = 2, and when i = n, j = n + 1. 2 So ni=1 (i + 1)2 = n+1 j=2 j . Pn Note that i and j etc. inside the summation only refer to the summation variables, i.e. i=1 ai ≡ P n j=1 aj these internal variables are called dummy variables. Theorem any sequences Pn 5 ForP Pn ai and bi , and any real number c n 1. i=m ai + i=m bi = i=m (ai + bi ) Pn Pn 2. i=m c · ai = c · ( i=m ai ) A common question about series is to find the value of the sum. 2
-4&8
1.3
Induction & Recursion
P. Danziger
Products
Given a sequence ai we may define the product of the sequence n Y
ai = am · am+1 · . . . · an
i=m
Example 6 5 Y i=1
i 1 2 3 4 5 1·2·3·4·5 1 = · · · · = = 1+i 2 3 4 5 6 2·3·4·5·6 6
Theorem 7 Given two sequence ai and bi ! n Y ai · i=m
n Y
bi
!
=
i=m
n Y
(ai · bi )
i=m
Definition 8 Given a positive integer n we define the factorial of n, denoted n!, to be given by: n! =
n Y
i = n · (n − 1) · (n − 2) · . . . · 2 · 1
i=1
Further we define 0! = 1. Example 9 1. 0! = 1, 6! = 720,
1! = 1, 7! = 5, 040,
2! = 2, 3! = 6, 4! = 24, 8! = 40, 320, 9! = 362, 880,
5! = 120, 10! = 3, 628, 800
2. Find 10! 8! 10! = 10 · 9 · 8 · 7 · 6 · 5 · 4 · 3 · 2 · 1 = 10 · 9 = 90. 8·7·6·5·4·3·2·1 8! 3. Find
n! (n − 2)!
n! = n · (n − 1) (n − 2)!
2
Induction (4.2)
Suppose we wish to prove ∀n ∈ Z, n ≥ a ⇒ pn , where a is some fixed number. (Usually 0 or 1, so this becomes ∀n ∈ N, pn or ∀n ∈ N+ , pn .) That is the property p is indexed in some way by n. Note that pn may involve quantifiers and hence other variables. We can prove a statement like this by using the method of induction. Consider the following two statements 3
-4&8
Induction & Recursion
P. Danziger
1. pa is true. 2. For every natural number k, with k ≥ a, if pk is true then pk+1 is true. If these two statements are both true then ∀n ∈ N, n ≥ a ⇒ pn will be true, by the domino effect. The first part is celled the base case. The second part is called the inductive step Thus every proof by induction looks like: Proof: By Induction on n Base Case Let n = a. Prove that pa is true. Inductive Step Let n = k, with k ≥ a. Assume pk (this is called the inductive hypothesis) .. . Hence pk+1 . In the inductive step it is helpful to keep in mind what pk+1 is.
3
Recursion
Basic problem of recursion: Given a recursively defined sequence ai find an analytical formula for it. We must do 2 things: 1. Find an explicit formula for ai (Educated guesswork) 2. Prove that this formula is correct (Induction) We do the first by examining the first few terms of the sequence and looking for a pattern. Example 10 Given b0 = 1, bn =
bn−1 for n > 1, find a formula for bn and prove it. 1 + bn−1 1 1 1 b3 = 1+3 1 = b0 = 1, b1 = 1+1 = 12 , b2 = 1+2 1 = 12 · 23 = 13 , 3 2 1 . Conjecture: bn = n + 1 Proof: By Induction on n Base Case: 1 . Let n = 0, b0 = 1 = 0 + 1 So true for n = 0. Inductive Step: Let n = k ≥ 0. bk = 1 k+1 1 Now bk+1 = bk = k+11 = 1 + bk 1 + k+1 So true for k + 1.
1 k+1 k+2 k+1
=
1 · k+1 = 1 . k+1 k+2 k+2
4
1 3 4 3
= 13 · 34 = 14 ,
-4&8
4
Induction & Recursion
P. Danziger
Strong Induction
The type of induction above is called weak induction. We prove the base case of n = a and then go on to prove the inductive step (pk ⇒ pk+1 ). But what if the proof of pk+1 actually requires not only that pk be true, but also that pi be true for every a ≤ i ≤ k? In strong induction we modify the steps somewhat. Base Case: Show that pa , pa+1 , . . . , pa+m−1 are all true, for some fixed m. Inductive Step: Let n = k, (k ≥ a + m). Assume pi is true for every a ≤ i < k. Show that pk follows. Note: It is customary to assume pi for all a ≤ i < k and show pk follows, rather than assuming pi for all a ≤ i ≤ k and showing pk+1 follows. Theorem 11 Let ai be the sequence defined by a0 = 12, a1 = 29, an = 5an−1 − 6an−2 , for n > 1. Prove that an = 5 · (3)n + 7 · (2)n for all n ≥ 0. Note that we cannot use weak induction since we need to go back 2 steps. Proof: By Induction on n Let pn be the statement an = 5 · (3)n + 7 · (2)n , where an is defined as above. Base Case: n = 0, 5 · 30 + 7 · 20 = 5 + 7 = 12 = a0 , so true for n = 0 n = 1, 5 · 31 + 7 · 21 = 15 + 14 = 29 = a1 , so true for n = 1 Inductive Step: Let n = k > 1. Assume pi is true for every 0 ≤ i < k. Now ak = 5ak−1 − 6ak−2 . But ak−1 = 5 · (3)k−1 + 7 · (2)k−1 and ak−2 = 5 · (3)k−2 + 7 · (2)k−2 , by the inductive hypothesis. Thus ak = 5(5 · (3)k−1 + 7 · (2)k−1 ) − 6(5 · (3)k−2 + 7 · (2)k−2 ) = (25 · (3)k−1 + 35 · (2)k−1 ) − (30 · (3)k−2 + 42 · (2)k−2 ) = (25 · (3)k−1 + 35 · (2)k−1 ) − (10 · (3)k−1 + 21 · (2)k−1 ) = (15 · (3)k−1 + 14 · (2)k−1 ) = (5 · 3 · (3)k−1 + 7 · 2 · (2)k−1 ) = (5 · (3)k + 7 · (2)k ) So pk is true. Theorem 12 Every integer greater than 1 is divisible by a prime number. 5
-4&8
Induction & Recursion
P. Danziger
To Prove: ∀n ∈ Z, n > 1 ⇒ ∃ p ∈ P Such that p | n. Proof: By induction on n Base Case Let n = 2. 2 is prime and 2 | 2, so the property holds for n = 2. Inductive Step: Let n = k, k > 2. Assume that all integers i, with 2 ≤ i < k, are divisible by a prime number. Consider k. Either k is prime or it is composite. If k is prime then k | k and we are done. If k is composite then ∃ r, s ∈ N+ such that k = rs, and r, s 6= 1. (Definition of composite) We have previously shown that ∀ a, b ∈ N+ , a | b ⇒ a ≤ b, so r ≤ k But s 6= 1, so r 6= k, thus 2 ≤ r < k. So by the inductive hypothesis there exists a prime p such that p | r. Now p | r and r | k, so p | k, by transitivity of divisibility. Theorem 13 (Existence part of UFT) ∀ n ∈ Z+ , with n > 1, ∃ ` ∈ Z+ such that ∃ p1 , . . . , p` ∈ P (pi distinct) and e1 , . . . , e` ∈ Z+ such that n = pe11 · . . . · pe` ` . Proof: By induction on n Base Case: Let n = 2. ` = 1, p1 = 2, e1 = 1, 2 = 21 , so true for n = 2. Inductive Step: Let n = k, k > 2 Assume true for that all integers i, with 2 ≤ i < k. Consider k. Either k is prime or it is composite. If k is prime take ` = 1, p1 = k and e1 = 1, then k = k 1 and we are done. If k is composite then as above ∃ r, s ∈ N+ such that k = rs and 1 < r, s < k. So by the inductive hypothesis ∃ `1 ∈ Z+ such that ∃ p1 , . . . , p`1 ∈ P and e1 , . . . , e`1 such e that r = pe11 · . . . · p`1`1 and ∃ `2 ∈ Z+ such that ∃ q1 , . . . , q`2 ∈ P and d1 , . . . , d`2 such that d
s = q1d1 · . . . · q`2`2 . Order the primes so that p1 = q1 , . . . , pm = qm , and pm+1 , . . . , p`1 , qm+1 , . . . , q`2 are all distinct. d e d e dm+1 em+1 ·. . .·q`2`2 Now k = rs = pe11 ·. . .·p`1`1 ·q1d1 ·. . .·q` `2 = pe11 +d1 ·. . .·pemm +dm ·pm+1 ·. . .·p`1`1 ·qm+1 So the theorem is true for k.
4.1
Towers of Hanoi
The following problem was posed by Edouard Lucas in 1883., in 1884 De Parville gave the colorful description. In the great temple at Benares, beneath the dome which marks the center of the world, rests a brass plate in which are fixed three diamond poles, each a cubit high and as thick as the body of a bee. On each of these poles, at the time of creation God placed 64 discs of pure gold, the largest disk resting on the brass plate and the others getting 6
-4&8
Induction & Recursion
P. Danziger
smaller and smaller up to the top one. This is the tower of Bramah. Day and night unceasingly the priests transfer the discs from one pole to another according to the fixed and immutable laws of Bramah, which require that the priest on duty must not move more than one disc at a time and that they must this disc on a pole so that there is no smaller disc below it. When the 64 discs shall have been thus transferred from the pole on which at the creation God placed them to one of the other poles, tower, temple and Brahmins alike will crumble to dust, and with a thunderclap the world will vanish. Do we have anything to worry about? i.e. How long will it take to transfer the disks? We can solve this problem recursively. Let mn be the number of moves needed to transfer a tower of n disks from one pole to another. Suppose that we have found the most efficient way to transfer k − 1 disks from one pole, one by one, to another, obeying the restriction that no disk may be placed on a smaller one. What is the most efficient way to move a tower of k disks from one pole to another? We will assume that A is the initial pole and C is the target pole. Step 1 Move the top k − 1 disks from pole A to pole B. By assumption we know the most efficient way of doing this. This takes mk−1 moves. Step 2 Move the bottom disk from pole A to pole C. This takes one move. Step 3 Move the k − 1 disks on pole B to pole C. By assumption we know the most efficient way of doing this. This takes mk−1 moves. Note that this reasoning has actually given us a recursive algorithm for solving the problem. We can see that mk = 2mk−1 + 1 and the number of moves needed to transfer one disk is clearly one. We wish to find an analytic formula for m1 = 1, mn = 2mn−1 + 1 for n > 1. Consider the first few terms. m1 = 1, m2 = 2 · 1 + 1 = 2 + 1 = 3, m3 = 2(2 + 1) + 1 = 22 + 2 + 1 = 7, m4 = 2(22 + 2 + 1) + 1 = 23 + 22 + 2 + 1 = 15, m5 = 2(23 + 22 + 2 + 1) + 1 = 24 + 23 + 22 + 2 + 1 = 31, So we might guess that mn =
Pn−1 i=0
2i . This is a geometric series with r = 2, and so
mn =
n−1 X i=0
2i =
2n − 1 = 2n − 1. 2−1
Theorem 14 The number of steps required to solve the Towers of Hanoi problem with n disks is 2n − 1. 7
-4&8
Induction & Recursion
P. Danziger
Proof: We have shown that the number of steps required, mn , is given by m1 = 1, mn = 2mn−1 + 1 for n > 1. We wish to show that mn = 2n − 1. We proceed by induction on n Base case Let n = 1. m1 = 1 = 21 − 1. So true for n = 1. Inductive Step: Let n = k and assume that mk = 2k − 1. Consider mk+1 = 2mk + 1 = 2(2k − 1) + 1, by the inductive hypothesis. But 2(2k − 1) + 1 = 2k+1 − 2 + 1 = 2k+1 − 1. So the hypothesis is true for k + 1. The original problem had 64 disks, 264 − 1 > 1.8 × 1019 . So even if the priests work quickly and transfer one disk every second it will take them approximately 584.5 billion years! Addendum Suppose we write a computer program to solve this problem. Suppose that this program, being very fast, can transfer one virtual disk every 10 microseconds (= 10−5 seconds), it will still take the program 5.845 million years to finish! Thus a seemingly simple calculation, even with a very fast machine, can take a prohibitively long time to complete.
8