mth202 Lecture21-Recursion
mth202 Lecture21-Recursion
2
EXERCISE
Now in order to find out the other values we will need the values of the
preceding .So we write these values here again
f(0) = -1, f(1)=2 f(n+1) = f(n) + 3 f(n - 1)
f(2) = -1
EXERCISE
0 if a b
Q ( a, b)
Q(a b, b) 1 if b a
(a) Find the value of Q(2,3) and Q(14,3)
(b) What does this function do? Find Q (3355, 7)
SOLUTION
(a) Q (2,3) = 0 since 2 < 3
5
Given Q(a,b) = Q(a-b,b) + 1 if b a
Now
Q (14, 3) = Q (11,3) + 1
= [Q(8,3) + 1] + 1 = Q(8,3) + 2
= [Q(5,3) + 1] + 2 = Q(5,3) + 3
= [Q(2,3) + 1] + 3 = Q(2,3) + 4
=0+4 ( Q(2,3) = 0)
=4
(b)
0 if a b
Q ( a, b)
Q(a b, b) 1 if b a
Each time b is subtracted from a, the value of Q is increased by 1. Hence
Q(a,b) finds the integer quotient when a is divided by b.
Thus Q(3355, 7) = 479
F2 = F1 + F0 = 1 + 1 = 2
F3 = F2 + F1 = 2 + 1 = 3
F4 = F3 + F2 = 3 + 2 = 5
F5 = F4 + F3 = 5 + 3 = 8
.
.
6
.
RECURRENCE RELATION
A recurrence relation for a sequence a0, a1, a2, . . . , is a formula that relates
each term ak to certain of its predecessors ak – 1, ak – 2, . . . , ak – i ,
where i is a fixed integer and k is any integer greater than or equal to i. The
initial conditions for such a recurrence relation specify the values of
a0 , a 1 , a 2 , . . . , a i – 1 .
EXERCISE
Find the first four terms of the following recursively defined sequence.
b1 = 2
bk = bk – 1 + 2 · k, for all integers k 2
SOLUTION
b1 = 2 (given in base step)
b2 = b1 + 2 · 2 = 2 + 4 = 6
b3 = b2 + 2 · 3 = 6 + 6 = 12
b4 = b3 + 2 · 4 = 12 + 8 = 20
Find the first five terms of the following recursively defined
sequence.
t0 = – 1, t1 = 1
tk = t k – 1 + 2 · t k – 2 , for all integers k 2
SOLUTION
SOLUTION
The sequence is given by the formula
bn = 5n
Substituting k for n we get
bk = 5k . . . . . (1)
Substituting k – 1 for n we get
bk – 1 = 5k – 1 . . . . . (2)
Multiplying both sides of (2) by 5 we obtain
5 · bk – 1 = 5 · 5k – 1
= 5k = bk using (1)
Hence bk = 5bk – 1 as required
EXERCISE
Show that the sequence 0, 1, 3, 7, . . . , 2 n – 1, . . . , for n 0, satisfies the
recurrence relation
dk = 3dk – 1 – 2dk – 2, for all integers k 2
SOLUTION
The sequence is given by the formula
dn = 2n – 1 for n 0
Substituting k – 1 for n we get dk – 1 = 2k – 1 – 1
Substituting k – 2 for n we get dk – 2 = 2k – 2 – 1
We want to prove that
dk = 3dk – 1 – 2dk – 2
R.H.S. = 3(2k – 1 – 1) – 2(2k – 2 – 1)
= 3 · 2k – 1 – 3 – 2 · 2 k – 2 + 2
= 3 · 2k – 1 – 2k – 1 – 1 8
= (3 – 1) · 2k – 1 – 1
= 2 · 2k – 1 – 1 = 2k – 1 = d = L.H.S.
THE TOWER OF HANOI
•m1 = 1
•mk = 2 mk – 1 + 1
m2 = 2 · m1 + 1 = 2 · 1 + 1 = 3
m3 = 2 · m2 + 1 = 2 · 3 + 1 = 7
m4 = 2 · m3 + 1 = 2 · 7 + 1 = 15
m5 = 2 · m4 + 1 = 2 · 15 + 1 = 31
m6 = 2 · m5 + 1 = 2 · 31 + 1 = 65
Note that
mn = 2n – 1
m64 = 264 – 1
584.5 billion years
9
USE OF RECURSION
10