top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Recursion: One of the most powerful programing techniques

Writer's picture: Abu Bin FahdAbu Bin Fahd

First, let's talk about the recursion theory. Look at the picture carefully. We want to sort the number Increasingly. We break the first box into two sub- boxes. And each sub box breaks into two sub-box. In the second stage we want to stop. That's called base point/value. Then we sorted these sub boxes. After that, each two sub box combined one with sort. Another two were the same. Finally, the last two boxes combined one with the final sort values. This is how the recursion works. That means for its convenience it breaks into some subsets and then combines again.


Let's dive into an example,


Factorial! (Normal function)

def factorial(n):
    temp = 1
    
    for i in range(2, n+1):
        temp = temp * i
    
    return temp

n = int(input())
result = factorial(n)
print(result)
input: 4
Ans: 24



Recursion is a function that calls itself.


Factorial! (Recursive function)

def factorial(n):
    if n == 1:        #base value
        return 1
    
    return n * factorial(n-1)

n = int(input())
result = factorial(n)
print(result)
input:4
Ans: 24

Let's Explain,


It is a widely used idea in programming to solve complex problems by breaking them down into simpler ones. We can think about a big box. For our convenience, we break down the box into sub box. If we need it more simple, we can break it into more sub box. And there is an endpoint. Endpoint means where we want to stop breaking the box. It is called base value/point. In recursion, we have to locate this base value in our programme.

4! = 4 x 3 x 2 x 1. Here 1 is the last value where we want to stop. So 1 is the base value.


return n * factorial(n -1)

=> 4 * factorial(4 -1)

=> 4 * factorial(3)

Here factorial(3) is a new function that calls itself. And this process is running until then equal to 1.

=> 4 * factorial(3)

=> 4 * 3 * factorial(3 -1) "return n * factorial(n -1)"

=> 4 * 3 * factorial(2)

...continue




0 comments

Recent Posts

See All

Comments


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page