Week-9 Data
Week-9 Data
RECURSION
What is Recursion?
Recursion in Nature
Base Case
Advantages of Recursion
1. Towers of Hanoi
2. Factorial of a Number
3. Fibonacci Sequence
4. Recursive Binary Search
Since the value of n is 3, the control goes to line 5 and the first recursive
call is pushed on to stack with n value = 3
def fact() :
if n == 1:
return n
else
return 3 * fact(2)
def fact() :
if n == 1:
return n
else
return 2 * fact(1)
def fact() :
if n == 1:
return n
else
return 3 * fact(2)
The following are the characteristics that all-recursive solutions must meet:
#In this recursive function base case is element is in the list OR when low
index # crosses high
else:
else:
return -1
x=6
if result != -1:
else:
Towers of Hanoi
if(disks == 1):
RECURSION
A function (or method) can call any other function (or method),
including itself.
A function that calls itself is known as a recursive function.
Recursion
Direct Indirect
Direct Recursion
def function( )
//……. function body
……..//
function( )
Indirect Recursion
The function calls another function which in turn calls the first function.
Example
def function1( )
//……. function body
……..// function2( )
def function2( )
//…….
function body
……..// function1( )
The following table outlines the key differences between recursion and
iteration:
Properties of a Recursion
Recursive Functions:Factorials
n! = n ∗ (n − 1) ∗(n − 2) ∗ . . . 1
The factorial function on different integer values:
0! = 1
1! = 2 ∗ 1
2! = 3 ∗ 2∗ 1
for n > 1, can be rewritten in terms of the previous equation:
0! = 1
1! = 1 ∗ (1 − 1)!
2! = 2 ∗ (2 − 1)!
3! = 3 ∗ (3 − 1)!
4! = 4 ∗ (4 − 1)!
5! = 5 ∗ (5 − 1)!
Since the function is defined in terms of itself and contains a base case, a
Recursive definition can be
def factorial(n):
if n==0:
return 1
return (n*factorial(n-1))
Recursive functions use something called “the call stack”. When a program
calls a function, that function goes on top of the call stack.
Each time when the code calls a function, that function gets added to the
stack.
Once the function ends, it gets taken off the list and the code returns to the
calling function.
Call stack determines the rules for the order that these function call will
return
The nth Fibonacci number can be computed by the recurrence relation (for
n > 0):
def fib(n):
if (n == 0):
return 0
if n == 1 or n == 2:
return 1
else:
return fib(n - 1) + fib(n - 2)
A function (or method) can call any other function (or method),
including itself. A function that calls itself is known as a recursive
function.
Example
def function( )
//……. function body
……..//
function( ) Recursive call
typically occurs in each recursive call when the larger problem is divided
into smaller parts.
Recursive Functions:Factorials
The factorial of a positive integer n can be used to calculate the number of
permutations of n elements. The function is defined as:
n! = n ∗ (n − 1) ∗(n − 2) ∗ 1
The factorial function on different integer values: 0! = 1
1! = 2 ∗ 1
2! = 3 ∗ 2∗ 1
for n > 1, can be rewritten in terms of the previous equation:
0! = 1
1! = 1 ∗ (1 − 1)!
2! = 2 ∗ (2 − 1)!
3! = 3 ∗ (3 − 1)!
4! = 4 ∗ (4 − 1)!
5! = 5 ∗ (5 − 1)!
Since the function is defined in terms of itself and contains a base case, a
Recursive definition can be
def factorial(n):
if n==0:
return 1
return (n*factorial(n-1))
The nth Fibonacci number can be computed by the recurrence relation (for
n > 0):
def fib(n):
if (n == 0): return 0
if n == 1 or n == 2: return 1
else:
return fib(n - 1) + fib(n - 2)
def factorial(n):
if n==0:
return 1
return (n*factorial(n-1))
OUTPUT
Enter a number to find its factorial value 4 Factorial of 4 is: 24
def fib(n):
if (n == 0):
return 0
if n == 1 or n == 2:
return 1
else:
return fib(n - 1) + fib(n - 2) n = int(input("Enter a number"))
print("Fibonacci series of %d numbers are :" % n, end=" ") for i in range(0,
n):
print(fib(i), end=" ")
OUTPUT
Enter a number 5
Fibonacci series of 5 numbers are : 0 1 1 2 3