2. Primarily, there are two types of recursive functions. Functions that call itself or two functions that call
each other mutually. The functions that call itself are direct recursive and when two functions call each
other mutually, then those functions are called indirect recursive functions.
Types of Recursive functions
3. DIRECT RECURSIVE FUNCTIONS
These functions are the same recursive functions that you have been learning till now. When
a function calls the same function again, you call it a direct function. Factorial is an example
of direct recursive .
4. INDIRECT RECURSIVE FUNCTIONS
In Indirect recursive functions, two functions mutually call each other wherein both the functions must
have a base case. Let's understand using a flowchart.
Here, function1 calls function2 which in turn calls function1.
5. STRING REVERSAL
• Given a string, I want a reversed version of that string.
print reverse(“Arial")
# => “lairA”
26. Countdown Algorithm
• If I want to countdown from 0, then
it’s over! Don’t do anything.
• If I want to countdown from N (> 0),
then count N, and countdown from
N-1.
29. EXAMPLE: EUCLIDEAN ALGORITHM
If you have 2 numbers,
then you subtract the smaller number
from the larger number, the GCD of
these two number stays the same.
32. EXAMPLE: EUCLIDEAN ALGORITHM
If you have 2 numbers,
then you subtract the smaller number from
the larger number, the GCD of these two
number stays the same.
Keep doing that until the two numbers equal
each other, then that number is the GCD.
43. # Python program to find the H.C.F of two input number
# define a function
def computeHCF(x, y):
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
a=int(input("Enter first
number:"))
b=int(input("Enter second
number:"))
GCD=gcd(a,b)
print("GCD is: ") print(GCD)
RECURSION - PROGRAM
46. RECURSIVE ALGORITHM
• Empty string is a palindrome.
• String with 1 character is a
palindrome.
• String that has a different first
character and last character is not a
palindrome.
47. RECURSIVE ALGORITHM
• Empty string is a palindrome.
• String with 1 character is a palindrome.
• String that has a different first character and last
character is not a palindrome.
• String that has a same first and last character is a
palindrome only if the string without first and last
character is a palindrome.
50. Fibonacci numbers Using Recursion
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, …
Algorithm
Fib(n)
1. If n =1 or n=2, then
2. return 1
3. Else
4. a = Fib(n-1)
5. b = Fib(n-2)
6. return a+b
51. Fibonacci numbers Using Recursion
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, …
Program
def fib(n):
if n <= 1:
return n
else:
return(fib(n-1) + fib(n-2))
nterms = int(input("enter a number"))
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(fib(i))
52. Binary Search Using Recursion
Algorithm
1. Find the midpoint of the array; this will be the element
at arr[size/2]. The midpoint divides the array into two
smaller arrays: lower half and upper half
2. Compare key to arr[midpoint] by calling the
user function cmp_proc.
3. If the key is a match, return arr[midpoint]; otherwise
4. If the array consists of only one element return NULL,
indicating that there is no match; otherwise
5. If the key is less than the value extracted
from arr[midpoint] search the lower half of the
array by recursively calling search; otherwise
6. Search the upper half of the array by
recursively
calling search.
NOTE:- For binary search all elements must be in order.
53. Binary Search Using Recursion
Program
def binarySearch (arr, first, last, x):
if last >= first:
mid =int( first + (last - first)/2)
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binarySearch(arr, first, mid-1, x)
else:
return binarySearch(arr, mid+1, last, x)
else:
return -1
arr = [ 1,3,5,6,7,8,10,13,14 ]
x = 10
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print ("Element is present at index %d" % result)
else:
print ("Element is not present in array")
54. ITERATION VS RECURSION
• The concept of recursion and iteration is pretty much the same. Both execute a set of instructions repeatedly to achieve
the desired output, but only the methodology differs.
• Simply said, recursive functions call itself during its execution, enabling the function to repeat itself several times.
• Whereas iterative functions follow a set of instructions, these instructions are repeated in a sequence a specified number of
times or until a condition is met.
55. ITERATION VS RECURSION
• Recursive functions are commonly used in computer science because they allow programmers to write efficient programs
using a minimal amount of code. The downside is that they can cause infinite loops and other unexpected results if not
written properly. This is one of the core difference between iterative and recursive functions.
• Iterative functions are the best bet when time complexity is an issue because the number of iterations is defined or can be
easily defined, which is not the case for recursive functions.
• The recursive function has a relatively smaller and shorter code than the iterative function.
56. ITERATION VS RECURSION
# ----- Recursion -----
# Method to find factorial of given number
using a recursion function
• def recursive_factorial(n):
if (n == 0):
return 1
return n * recursive_factorial(n - 1)
# ----- Iteration -----
# Method to find the factorial of a given
number using an iteration function
• def iterative_factorial(n):
product = 1
for i in range(1, n + 1):
product = product * i
return product
57. RECURSION VS ITERATION
● Advantages
○ It can reduce time complexity
○ It adds clarity and reduce your time to write and
debug the code
○ It is better in problem based on
tree traversals / structures
● Disadvantages
○ It uses more memory for the stack
○ It can be slow, due to the overhead of maintaining
stack
58. 1 Recursive Python function to find sum of natural numbers.
2. Recursive Python function to find sum of even numbers.
3. Recursive Python function to find sum of odd numbers.
4. Recursive Python function to find sum of fib series.
5. Recursive Python function to find given number is prime or not.
6. Write and test a recursive function max to find the largest number
in a list(array of numbers).
EXERCISE