Notes On RecursiveFunctions
Notes On RecursiveFunctions
RECURSIVE DEFINITIONS
Different choices of the ai and of the expression e give rise to different functions.
Ex.1
Fibonacci function f : Z1 Z
(BS) f (1) = 1 f (2) = 1
(RS) n > 2 f (n) = f (n 1) + f (n 2)
Ex.2
Pn
i=1 ai where ai is an expression depending on i (e.g. ai = (2i 1) is a
recursive function.
(BS) f (1) = a1
Pn
(RS) n > 2 f (n) = i=1 ai = a1 + a2 + . . . + an1 + an = f (n 1) + an
Ex.3
Qn
i=1ai where ai is an expression depending on i (e.g. ai = 2i is a recursive
function.
(BS) f (1) = a1
Qn
(RS) n > 2 f (n) = i=1 ai = a1 a2 . . . an1 an = f (n 1) an
We assume
Pn Qn
i=m ai = 0 if m > n i=m ai = 0 if m > n
Ex.4
(BS) f (0) = 3
(RS) n > 0 f (n) = 2f (n 1) + 3
1
Find f (5)
There are two ways of finding the value of f (5)
By rewriting
f (5) = 2f (4) + 3 = 2(2f (3) + 3) + 3 = 4f (3) + 9 = 4(2f (2) + 3) + 9 =
8f (2) + 21 = 8(2f (1) + 3) + 21 = 16f (1) + 45 = 16(2f (0) + 3) + 45 =
32f (0) + 93 = 32 3 + 93 = 96 + 93 = 189
By iteration
We first find the values of f (0) . . . f (4) and we substitute, operating, in
the expression e of the definition.
f (0) = 3
f (1) = 2f (0) + 3 = 2 3 + 3 = 9
f (2) = 2f (1) + 3 = 2 9 + 3 = 21
f (3) = 2f (2) + 3 = 2 21 + 3 = 45
f (4) = 2f (3) + 3 = 2 45 + 3 = 93
f (5) = 2f (4) + 3 = 2 93 + 3 = 189
Recursion and induction are very closely related. Very often, we prove prop-
erties of recursive functions by induction.
For example, let f : Z1 Z be the Fibonacci function.
(BS) f (1) = 1 f (2) = 1
(RS) n > 2 f (n) = f (n 1) + f (n 2)
We are going to prove the following property by induction.
n Z3 f (1)2 + . . . + f (n)2 = f (n)f (n + 1)
2
We extend this definition to functions with several arguments:
Ex.1
+ : N N N
(B) +(m, 0) = m e0 (..b..) = b
(R) +(m, s(n)) = s(+(m, n)) e(..b..n..f (c, i)..) = s(f (b, n))
Ex.2
: N N N
(B) (m, 0) = 0 e0 (..b..) = 0
(R) (m, s(n)) = m + (m, n) e(..b..n..f (c, i)..) = b + f (b, n)
3
3. f : B Zm C A is defined recursively as follows:
Basis Step: Let l m, l Zm We define for all m n l
f (b, m, c) = em (..b..c..)
.
.
f (b, l, c) = el (..b..c..)