105. Recursive definition of functions
Many functions are defined in such a way that the value at one input is defined in terms of other values of the function. Such a definition is called recursive.
105.1.1 Example One way of defining the function $F\colon \mathrm{N} \to \mathrm{N}$ for which $F (n) =$ $2^{n}$ would be to say
$$\begin{cases} F(0) = 1 \\ F (n + 1) = 2\cdot F (n) \end{cases} \tag{105.1}$$
for all $n \in \mathrm{N}$.
Programs 105.1 and 105.2 give Pascal functions which calculate $2^{n}$.
FUNCTION TWOREC(N:INTEGER):INTEGER;
BEGIN
IF N=0 THEN TWOREC := 1
ELSE TWOREC := 2*TWOREC(N-1)
END;
Program 105.1: Program for $2^{n}$
Program 105.1 simply copies Definition 105.1. Since the function TWOREC calls itself during its execution, this program is also said to be recursive. Program 105.2 is a translation of Definition 105.1 which avoids the function calling itself. Since it is implemented by a loop it is called an iterative implementation of the function.
105.1.2 Remark Many common algorithms are easily to define recursively, so the study of recursively-defined functions and how to implement them is a major part of computer science. Very often, the iterative implementation like Program 105.2 is to be preferred to the recursive one, but in complicated situations it is not always easy to transform the recursive definition into an iterative one. In some applications, for example in writing programs to parse expressions, the recursively written program may be the preferred method for writing the first attempt, since the iterative version can be much harder to understand and debug.
FUNCTION TWOIT(N:INTEGER):INTEGER; VAR COUNT:INTEGER;
BEGIN
COUNT := 0; POWER := 1;
(*POWER is a integer variable declared in
the program containing this procedure.*)
WHILE COUNT<N DO
BEGIN
POWER := 2*POWER;
COUNT := COUNT+1
END
TWOIT := POWER
END;
Program 105.2: Another program for $2^{n}$
105.1.3 Exercise Find the values for $n= 1$ through 5 of the functions defined as follows:
a) $F (0) =- 3,F (n + 1) = (n + 1)F (n)$
b) $F (1) = 1,F (n ) =n^{2} +F (n-1)$
c) $F (n) =\begin{cases} 0 & \text{if } 3|n \\ 1 +F (n+ 1) & \text{otherwise} \end{cases}$
d) $F (0) = 1,F (1) = 3,F (n ) =F (n- 1) +F (n-2)$
e) $F (1) = 0,F (2) = 1,F (n ) = (n-1) \bigl(F (n- 1) +F (n-2)\bigr)$
(Answer on page 249.)
105.1.4 Example For a fixed sequence $\{a_{k}\}_{k\in\mathrm{N}^{+}}$,
$$F (n) = \sum_{k=1}^{n} a_{k}$$
is a function from $\mathrm{N}^{+}$ to $\mathrm{N}^{+}$ which has a natural definition by induction:
$$\begin{cases} \sum_{k=1}^{1} a_{k} =a_{1} \\ \sum_{k=1}^{n+1}a_{k} =a_{n+1}+\sum_{k=1}^{n}a_{k} \end{cases} \tag{105.2}$$
105.1.5 Example The product has a similar definition:
$$\begin{cases} \prod_{k=1}^{1} a_{k} =a_{1} \\ \prod_{k=1}^{n+1}a_{k} =a_{n+1} \cdot \bigl(\prod_{k=1}^{n}a_{k}\bigr) \end{cases} \tag{105.3}$$
105.1.6 The factorial function A particularly important function which can be defined by induction is the factorial function. Its value at $n$ is denoted $n$! and it is defined this way:
$$\begin{cases} 0! = 1 \\ (n+ 1)! = (n + 1)\cdot n! \end{cases} \tag{105.4}$$
Thus for $n > 0,n$! = $\prod_{k=1}^{n}k$; you can prove this by induction because both $n$! and the product are defined by induction (Exercise 105.2.1). The factorial function will be used in various combinatorial formulas in later sections.
105.2 Proofs involving inductively defined functions
Defining a function by induction makes it convenient to prove things about it by induction. For example, let us use induction to prove that $n$! $> 2^{n}$ for $n >3$. We start the induction at $n= 4$. Then 4! = 24 and $2^{4}= 16$, so the statement is true for $n= 4$. For the induction step, suppose $n$! $> 2^{n}$ and $n \geq 4$. It is necessary to prove that $(n+ 1)$! $> 2^{n+1}$. Both these functions are defined by induction, so we can apply their definitions and the induction hypothesis to get
$$(n+ 1)! = (n + 1)\cdot n! > 2\cdot n! \geq 2\cdot 2^{n} = 2^{n+1}$$
as required.
105.2.1 Exercise Prove directly from the inductive definition of $n$! that $n$! = $\prod_{k=1}^{n}k$ for all positive integers $n$. (Answer on page 249.)
105.2.2 Exercise Prove that for all integers $n > 0, 2^{n} \leq 2(n$!).
105.2.3 Exercise Find constants $C$ and $D$ for which for all integers $n > 0, 3^{n} \leq$ $C (n$!) and $4^{n} \leq D (n$!). Prove your answers are correct.
Pages 157–159 of the book (PDF).