Recursion
Recursion is a common mathematical and programming concept. It means that a function calls itself. This
technique is used for solving large computational problem by repeatedly applying the same procedure to reduce
it to successive smaller problems.
Recursion refers to a programming technique in which a function calls itself either directly or indirectly.
Recursion can be:
1. direct recursion- if function calls itself directly from its body.
example
def fun(): def recurse() :
fun() recurse()
2. indirect recursion – if function calls another function, which calls its caller function.
example
def fun() :
recurse()
def recurse() :
fun()
Iteration vs Recursion
Iteration Recursion
It makes code longer comparatively. It makes code short and simple
It is faster than recursion. It is slower than Iteration due to
overhead of multiple functions calls.
As it do not handle multiple function
calls need not maintain stack.
It maintains stack to handle multiple
function calls.
All recursive problems can be solved
iteratively.
All iterative problems can be solved
recursively.
Every recursive function has two parts:
1. Base case/ stopping case- there can be one or more base cases. It
must have a case (if) whose result is known. ( eg. Factorial of 0 is 1,
sum of 0 and n is n, 0th power of any base is 1 and any power to the base
0 is 0). If BASE case is not provided infinite recursion will occur.
2. Recursive Case /indicative case - providing solution using recursive
call to same function.
** Discuss problems like
a^b, sum of natural n nos, n!, Fibonacci no,GCD,
binary search
# program to find a^b
def power(a,b):
if b==0:
return 1
elif a==0:
return 0
elif b==1:
return a
else:
return
a*power(a,b-1)
print(power(2,4))
def factorial_recursive(n):
# Base case: 1! = 1
if n == 1:
return 1
# Recursive case: n! = n * (n-1)!
else:
return n * factorial_recursive(n-1)
Print(factorial_recursive(n))
def factorial(n):
Fact=1
If n==1:
return fact
else:
For i in range(1,n+1):
Fact*=I
Return fact

Python recursion

  • 2.
    Recursion Recursion is acommon mathematical and programming concept. It means that a function calls itself. This technique is used for solving large computational problem by repeatedly applying the same procedure to reduce it to successive smaller problems. Recursion refers to a programming technique in which a function calls itself either directly or indirectly. Recursion can be: 1. direct recursion- if function calls itself directly from its body. example def fun(): def recurse() : fun() recurse() 2. indirect recursion – if function calls another function, which calls its caller function. example def fun() : recurse() def recurse() : fun()
  • 3.
    Iteration vs Recursion IterationRecursion It makes code longer comparatively. It makes code short and simple It is faster than recursion. It is slower than Iteration due to overhead of multiple functions calls. As it do not handle multiple function calls need not maintain stack. It maintains stack to handle multiple function calls. All recursive problems can be solved iteratively. All iterative problems can be solved recursively.
  • 4.
    Every recursive functionhas two parts: 1. Base case/ stopping case- there can be one or more base cases. It must have a case (if) whose result is known. ( eg. Factorial of 0 is 1, sum of 0 and n is n, 0th power of any base is 1 and any power to the base 0 is 0). If BASE case is not provided infinite recursion will occur. 2. Recursive Case /indicative case - providing solution using recursive call to same function. ** Discuss problems like a^b, sum of natural n nos, n!, Fibonacci no,GCD, binary search
  • 5.
    # program tofind a^b def power(a,b): if b==0: return 1 elif a==0: return 0 elif b==1: return a else: return a*power(a,b-1) print(power(2,4))
  • 6.
    def factorial_recursive(n): # Basecase: 1! = 1 if n == 1: return 1 # Recursive case: n! = n * (n-1)! else: return n * factorial_recursive(n-1) Print(factorial_recursive(n)) def factorial(n): Fact=1 If n==1: return fact else: For i in range(1,n+1): Fact*=I Return fact