SlideShare a Scribd company logo
2
Most read
4
Most read
RECURSION
With Python
Made by
Bima Sudarsono Adinsa 1906399083
Dennis Al Baihaqi Walangadi 1906400141
Muhammad Faisal Adi Soesatyo 1906293184
Muhammad Ivan Radman 1906399114
With assistance of Mr. Fariz Darari, S.Kom, M.Sc., Ph.D.
Recursion
What is recursion?
Recursion is a computer programming
technique involving the use of
procedure, subroutine, function, or
algorithm that calls itself either
directly or indirectly
https://xkcd.com/1516/
RecursionComponents
01
Recursive case
Part where the code calls it self
Base case
Base case is the condition that
stops recursion from continuing
on forever
Recursion consist of
02
!
Recursion without
Base case will result
Recursion Error
When a recursion doesn’t have
base case, it will run infinitely
and reached maximum depth
Example
How a particular problem is
solved using recursion?
The idea is to represent a problem in terms of one or more smaller problems,
and add one or more base conditions that stop the recursion.
For example, we can use recursion to solve factorials. Let’s solve 10!
10! means 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1
But 9! also means 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1
Thus, 10! Can be written as 10 x 9!
That means it can be translated to:
n! = n x (n-1)!
So on python we can write ...
Example
def factorial(num):
# Base case 0! = 1
if num == 0:
return 1
# Recursive case
else:
return num * factorial(num
- 1)
Example usage : Factorial in Python
Because 0! = 1, and factorial
doesn’t have negative value
Recursion case, when the
number is greater than 0,
multiply the number by the
number behind it
Example
Example usage : Fibonacci in Python
Fibonacci is a set of numbers that starts with a one or a zero, followed by a
one, and proceeds based on the rule that each number (called a Fibonacci
number) is equal to the sum of the preceding two numbers.
If the Fibonacci sequence is denoted F(n), where n is the first term in the
sequence, the following equation obtains for n = 0, where the first two terms are
defined as 0 and 1 by convention:
F (0) = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...
In some texts, it is customary to use n = 1. In that case, the first two terms are
defined as 1 and 1 by default, and therefore:
F (1) = 1, 1, 2, 3, 5, 8, 13, 21, 34 …
So, in python we can write ...
Example
Example usage : Fibonacci in Python
def fib(n):
# Base case n = 0 or n = 1
if n == 0 or n == 1:
return n
# Recursive case
else:
return fib(n - 1) + fib(n
- 2)
Because based on the
theory, fibonacci sequence
starts from 0 or 1
if we want to get the value of
n, we have to sum the value of
n-1 with value of n-2
This algorithm will return a fibonacci number on index n
RecvsIter
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
CREDITS: This presentation template was created
by Slidesgo, including icons by Flaticon, and
infographics & images by Freepik.
References
● https://www.geeksforgeeks.org/recursion/
● https://www.cs.utah.edu/~germain/PPS/Topics/recursion.html
● https://www.khanacademy.org/computing/computer-
science/algorithms/recursive-algorithms/a/recursion
● Punch, William F. ,Richard Enbody, Practice of Computing Using Python,
3rd Edition (2017)
● https://medium.com/@williambdale/recursion-the-pros-and-cons-76d32d75973a
● https://stackoverflow.com/questions/5250733/what-are-the-advantages-and-
disadvantages-of-recursion
● https://desain.cs.ui.ac.id/static/img/asset/logo.zip

More Related Content

What's hot (20)

PPTX
07. Virtual Functions
Haresh Jaiswal
 
PPTX
Polymorphism in c++(ppt)
Sanjit Shaw
 
PPTX
class and objects
Payel Guria
 
PPTX
Introduction to-python
Aakashdata
 
PPTX
Bruteforce algorithm
Rezwan Siam
 
PPTX
Java(Polymorphism)
harsh kothari
 
PPTX
Queue Implementation Using Array & Linked List
PTCL
 
PPT
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
PPTX
Types of Statements in Python Programming Language
Explore Skilled
 
PPTX
Oop c++class(final).ppt
Alok Kumar
 
PPT
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
PPT
Algorithm analysis
sumitbardhan
 
PPTX
queue & its applications
somendra kumar
 
PPTX
Polymorphism in java
Elizabeth alexander
 
PPTX
Iterarators and generators in python
Sarfaraz Ghanta
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPTX
Presentation on Segmentation
Priyanka bisht
 
PPT
Time complexity.ppt
YekoyeTigabuYeko
 
PPTX
Daa unit 2
jinalgoti
 
PPTX
Abstract class in c++
Sujan Mia
 
07. Virtual Functions
Haresh Jaiswal
 
Polymorphism in c++(ppt)
Sanjit Shaw
 
class and objects
Payel Guria
 
Introduction to-python
Aakashdata
 
Bruteforce algorithm
Rezwan Siam
 
Java(Polymorphism)
harsh kothari
 
Queue Implementation Using Array & Linked List
PTCL
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Types of Statements in Python Programming Language
Explore Skilled
 
Oop c++class(final).ppt
Alok Kumar
 
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Algorithm analysis
sumitbardhan
 
queue & its applications
somendra kumar
 
Polymorphism in java
Elizabeth alexander
 
Iterarators and generators in python
Sarfaraz Ghanta
 
Presentation on Segmentation
Priyanka bisht
 
Time complexity.ppt
YekoyeTigabuYeko
 
Daa unit 2
jinalgoti
 
Abstract class in c++
Sujan Mia
 

Similar to Recursion with Python [Rev] (20)

PPTX
Recursion with python
BimaSudarsonoAdinsa
 
PDF
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
arijitghosal14
 
PDF
Iteration, induction, and recursion
Mohammed Hussein
 
PDF
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Professor Lili Saghafi
 
PDF
Recursion CBSE Class 12
chinthala Vijaya Kumar
 
PDF
01 Notes Introduction Analysis of Algorithms Notes
Andres Mendez-Vazquez
 
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
PDF
Introduction to Python and Matplotlib
François Bianco
 
PPTX
1. control structures in the python.pptx
DURAIMURUGANM2
 
PPTX
Recursion vs. Iteration: Code Efficiency & Structure
cogaxor346
 
PPTX
lecture4-recursion.pptx
Lizhen Shi
 
PDF
Fibonacci Function Gallery - Part 1 (of a series) - with minor corrections
Philip Schwarz
 
PDF
Recursion.pdf
Flavia Tembo Kambale
 
PDF
Dynamic programing
AniketSingh609353
 
PDF
Numerical differentation with c
Yagya Dev Bhardwaj
 
PDF
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
PPT
Recursion C programming exercises_ Recursion - w3resource.ppt
Carlos701746
 
PPTX
Introduction to Dynamic Programming.pptx
PochupouOwo
 
PPT
Partial compute function
Rajendran
 
Recursion with python
BimaSudarsonoAdinsa
 
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
arijitghosal14
 
Iteration, induction, and recursion
Mohammed Hussein
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Professor Lili Saghafi
 
Recursion CBSE Class 12
chinthala Vijaya Kumar
 
01 Notes Introduction Analysis of Algorithms Notes
Andres Mendez-Vazquez
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
Introduction to Python and Matplotlib
François Bianco
 
1. control structures in the python.pptx
DURAIMURUGANM2
 
Recursion vs. Iteration: Code Efficiency & Structure
cogaxor346
 
lecture4-recursion.pptx
Lizhen Shi
 
Fibonacci Function Gallery - Part 1 (of a series) - with minor corrections
Philip Schwarz
 
Recursion.pdf
Flavia Tembo Kambale
 
Dynamic programing
AniketSingh609353
 
Numerical differentation with c
Yagya Dev Bhardwaj
 
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
Recursion C programming exercises_ Recursion - w3resource.ppt
Carlos701746
 
Introduction to Dynamic Programming.pptx
PochupouOwo
 
Partial compute function
Rajendran
 
Ad

Recently uploaded (20)

PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PDF
July Patch Tuesday
Ivanti
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
July Patch Tuesday
Ivanti
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Top Managed Service Providers in Los Angeles
Captain IT
 
Ad

Recursion with Python [Rev]

  • 1. RECURSION With Python Made by Bima Sudarsono Adinsa 1906399083 Dennis Al Baihaqi Walangadi 1906400141 Muhammad Faisal Adi Soesatyo 1906293184 Muhammad Ivan Radman 1906399114 With assistance of Mr. Fariz Darari, S.Kom, M.Sc., Ph.D.
  • 2. Recursion What is recursion? Recursion is a computer programming technique involving the use of procedure, subroutine, function, or algorithm that calls itself either directly or indirectly https://xkcd.com/1516/
  • 3. RecursionComponents 01 Recursive case Part where the code calls it self Base case Base case is the condition that stops recursion from continuing on forever Recursion consist of 02 ! Recursion without Base case will result Recursion Error When a recursion doesn’t have base case, it will run infinitely and reached maximum depth
  • 4. Example How a particular problem is solved using recursion? The idea is to represent a problem in terms of one or more smaller problems, and add one or more base conditions that stop the recursion. For example, we can use recursion to solve factorials. Let’s solve 10! 10! means 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 But 9! also means 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 Thus, 10! Can be written as 10 x 9! That means it can be translated to: n! = n x (n-1)! So on python we can write ...
  • 5. Example def factorial(num): # Base case 0! = 1 if num == 0: return 1 # Recursive case else: return num * factorial(num - 1) Example usage : Factorial in Python Because 0! = 1, and factorial doesn’t have negative value Recursion case, when the number is greater than 0, multiply the number by the number behind it
  • 6. Example Example usage : Fibonacci in Python Fibonacci is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. If the Fibonacci sequence is denoted F(n), where n is the first term in the sequence, the following equation obtains for n = 0, where the first two terms are defined as 0 and 1 by convention: F (0) = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ... In some texts, it is customary to use n = 1. In that case, the first two terms are defined as 1 and 1 by default, and therefore: F (1) = 1, 1, 2, 3, 5, 8, 13, 21, 34 … So, in python we can write ...
  • 7. Example Example usage : Fibonacci in Python def fib(n): # Base case n = 0 or n = 1 if n == 0 or n == 1: return n # Recursive case else: return fib(n - 1) + fib(n - 2) Because based on the theory, fibonacci sequence starts from 0 or 1 if we want to get the value of n, we have to sum the value of n-1 with value of n-2 This algorithm will return a fibonacci number on index n
  • 8. RecvsIter 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
  • 9. CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik. References ● https://www.geeksforgeeks.org/recursion/ ● https://www.cs.utah.edu/~germain/PPS/Topics/recursion.html ● https://www.khanacademy.org/computing/computer- science/algorithms/recursive-algorithms/a/recursion ● Punch, William F. ,Richard Enbody, Practice of Computing Using Python, 3rd Edition (2017) ● https://medium.com/@williambdale/recursion-the-pros-and-cons-76d32d75973a ● https://stackoverflow.com/questions/5250733/what-are-the-advantages-and- disadvantages-of-recursion ● https://desain.cs.ui.ac.id/static/img/asset/logo.zip