X X X X: Recursion

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Recursion

In mathematics and computer science a class of objects or methods defined by a simple base case (or
cases) and rules to reduce all other cases toward the base case. For example, the following is a recursive
definition of a person's ancestors:

 One's parents are one's ancestors (base case).


 The parents of one's ancestors are also one's ancestors (recursion step).

Self Recursion

Self recursion is one in which objects/functions are defined in terms of itself; Care must be taken to avoid
infinite nesting.

For example exponents

When we calculate an exponent, say x4, we multiply x by itself four times. If we have x5, we multiply x by
itself five times.

If we want a recursive definition of exponents, we need to define the action of taking exponents in terms
of itself.

So we note that x4 for example, is the same as x3 × x.

Then, what is x3?

x3 is the same as x2 x x.

We can proceed like this up to x0 = 1.

By generalizing recursively,

xn = x(xn-1) where x0 = 1

Recursively Defined Sets

Example: the natural numbers


The canonical example of a recursively defined set is given by the natural numbers.
0 is in 
If n is in , then n + 1 is in 
The set of natural numbers is the smallest set of real numbers satisfying the previous two properties.

In terms of function,

A recursive definition of a function defines values of the functions for the given inputs in terms of the
values of the same function for other inputs. 

Or
It is a function from which values of the function can be calculated in a finite number of steps

Example: Factorial of a number

The factorial function n! is defined by the rules

0! = 1
n! = n(n-1)!

This definition is valid because, for all n, the recursion eventually reaches  0!. The definition also
describes how to construct the function n!, starting from n = 0 and proceeding onwards with n = 1, n = 2, n
= 3 etc..

Let us work on 3!

3! = 3(3-1)! = 3 x 2!

=3 x 2(2-1)!

=3 x 2 x 1!

 = 3 x 2 x 1(1-1)!

= 3 x 2 x 1 x 0! 

=3 x 2 x 1 x 1 since 0! = 1

 =6

Therefore, 3! = 6

Similarly 5! Can be evaluated as


5! = 5 x 4!
4!= 4 x 3!
3!= 3 x 2!

There 5! = 120

Fibonacci series

The Fibonacci series is defined by the rules

First number of the series fib (0) = 0

Second number of the series fib (1) = 2

Then the series is recursively defined as for all integers n >1 fib (n) = fib (n-1) + fib (n – 2).

Example: Given, a recursively defined function defined by


f(0) = 2

f(n) = f(n – 1) + 5
Let us calculate the values of this function.

f(0) = 2

f(1) = f(1 – 1) + 5 = f(0) + 5 = 2 + 5 = 7

f(2) = f(2 – 1) + 5 = f(1) + 5 = 7 + 5 = 12

f(3) = f(3 – 1) + 5 = f(2) + 5 = 12 + 5 = 17

f(4) = f(4 – 1) + 5 = f(3) + 5 = 17 + 5 = 22

This recursively defined function is equivalent to the explicitly defined function f (n) = 2n + 5.

However, the recursive function is defined only for non-negative integers.

You might also like