Introduction
Welcome to my 30th blog post. Recursions are functions only. But if we want to call the function ‘a’ from the same function ‘a’ then it is called a Recursion. Using recursion we can call the same function again and perform some repetitive calculations on it. Recursion finds its application in some famous mathematical problems like finding the factorial of a number or calculating the Fibonacci sequence. Let's dive into the details of recursion in this blog and write some bunch of code.
So let's get started......
What is Recursion?
Recursions are functions only. But if we want to call the function ‘a’ from the same function ‘a’ then it is called a Recursion. In technical terms, Recursion is the process of defining something in terms of itself.
Python Recursive Function
In Python, we know that a function can call other functions. It is even possible for the function to call itself. These types of construct are termed recursive functions.
Let us understand how the concept of recursion works using 2 examples viz:
Factorial function
Fibonacci sequence
Recursion Example 1 - Factorial function
# factorial(7) = 7*6*5*4*3*2*1
# factorial(6) = 6*5*4*3*2*1
# factorial(5) = 5*4*3*2*1
# factorial(4) = 4*3*2*1
# factorial(0) = 1
# factorial(n) = n * factorial(n-1)
def factorial(n):
if (n == 0 or n == 1):
return 1
else:
return n * factorial(n - 1)
a = input("Enter the number: ")
print(factorial(int (a)))
Output -
Enter the number: 5
120
Recursion Example 1 - Fibonacci Sequence
#fibonacci Sequence using recursion
#0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, .......
f0 = 0
f1 = 1
#fn = fn-1 + fn-2
def fibonacci(n):
if n == 0:
return f0
elif n==1:
return f1
else :
return fibonacci(n-1) + fibonacci(n-2)
a = input("Enter the number: ")
#print(factorial(int (a)))
print(fibonacci(int (a)))
Output -
Enter the number: 6
8
Resources Used
You can watch the video of Day#30 by clicking on the below link 👇👇👇👇👇
Conclusion
Thanks, guys for going through this blog post. On day #30 we learned about recursion in python and its 2 application in a mathematical model to calculate factorial of a number and the Fibonacci sequence Learnt something interesting and fun stuff about python today.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding. This is it for Day #30
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.