X X X X: Recursion
X X X X: Recursion
X X X X: 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:
Self Recursion
Self recursion is one in which objects/functions are defined in terms of itself; Care must be taken to avoid
infinite nesting.
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.
x3 is the same as x2 x x.
By generalizing recursively,
xn = x(xn-1) where x0 = 1
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
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
There 5! = 120
Fibonacci series
Then the series is recursively defined as for all integers n >1 fib (n) = fib (n-1) + fib (n – 2).
f(n) = f(n – 1) + 5
Let us calculate the values of this function.
f(0) = 2
This recursively defined function is equivalent to the explicitly defined function f (n) = 2n + 5.