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
 
PPTX
DSA. Data structure algorithm dRecursion.pptx
siddiqsid0006
 
PPTX
lecture4-recursion.pptx
Lizhen Shi
 
PDF
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Professor Lili Saghafi
 
PDF
Recursion CBSE Class 12
chinthala Vijaya Kumar
 
PDF
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
arijitghosal14
 
PPTX
It is a way of programming or coding technique, in which a function calls it...
ssuser5ecd1a
 
PDF
Python Programming unit5 (1).pdf
jamvantsolanki
 
PPTX
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
usha raj
 
PPTX
Enhanced_Recursion_Presennnnntation.pptx
AliAbbas574107
 
PDF
6-Python-Recursion.pdf
AshishPalandurkar2
 
PDF
PyOhio Recursion Slides
Rinita Gulliani
 
PPTX
Recursion-in-Python for class third.pptx
raju909783
 
PPT
14 recursion
Himadri Sen Gupta
 
PPTX
Recursion
Esther Leytush
 
PPTX
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
PPTX
Python recursion
ToniyaP1
 
PDF
Recursion (in Python)
saverioperugini
 
PPT
Recursion C programming exercises_ Recursion - w3resource.ppt
Carlos701746
 
PPTX
Recursive Algorithms with their types and implementation
Ahmad177077
 
Recursion with python
BimaSudarsonoAdinsa
 
DSA. Data structure algorithm dRecursion.pptx
siddiqsid0006
 
lecture4-recursion.pptx
Lizhen Shi
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Professor Lili Saghafi
 
Recursion CBSE Class 12
chinthala Vijaya Kumar
 
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
arijitghosal14
 
It is a way of programming or coding technique, in which a function calls it...
ssuser5ecd1a
 
Python Programming unit5 (1).pdf
jamvantsolanki
 
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
usha raj
 
Enhanced_Recursion_Presennnnntation.pptx
AliAbbas574107
 
6-Python-Recursion.pdf
AshishPalandurkar2
 
PyOhio Recursion Slides
Rinita Gulliani
 
Recursion-in-Python for class third.pptx
raju909783
 
14 recursion
Himadri Sen Gupta
 
Recursion
Esther Leytush
 
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
Python recursion
ToniyaP1
 
Recursion (in Python)
saverioperugini
 
Recursion C programming exercises_ Recursion - w3resource.ppt
Carlos701746
 
Recursive Algorithms with their types and implementation
Ahmad177077
 
Ad

Recently uploaded (20)

PPTX
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
PDF
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
July Patch Tuesday
Ivanti
 
PPTX
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
July Patch Tuesday
Ivanti
 
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
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