SlideShare a Scribd company logo
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

Functions in python
Functions in pythonFunctions in python
Functions in python
Ilian Iliev
 

What's hot (20)

Floating point representation
Floating point representationFloating point representation
Floating point representation
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Python programming : Control statements
Python programming : Control statementsPython programming : Control statements
Python programming : Control statements
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 

Similar to Recursion with Python [Rev]

35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
1. control structures in the python.pptx
1. control structures in the python.pptx1. control structures in the python.pptx
1. control structures in the python.pptx
DURAIMURUGANM2
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
Sure Interview
 

Similar to Recursion with Python [Rev] (20)

Recursion with python
Recursion with pythonRecursion with python
Recursion with python
 
Iteration, induction, and recursion
Iteration, induction, and recursionIteration, induction, and recursion
Iteration, induction, and recursion
 
Daa
DaaDaa
Daa
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
 
Recursion CBSE Class 12
Recursion CBSE Class 12Recursion CBSE Class 12
Recursion CBSE Class 12
 
01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
 
Introduction to Python and Matplotlib
Introduction to Python and MatplotlibIntroduction to Python and Matplotlib
Introduction to Python and Matplotlib
 
1. control structures in the python.pptx
1. control structures in the python.pptx1. control structures in the python.pptx
1. control structures in the python.pptx
 
Recursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & StructureRecursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & Structure
 
lecture4-recursion.pptx
lecture4-recursion.pptxlecture4-recursion.pptx
lecture4-recursion.pptx
 
Recursion.pdf
Recursion.pdfRecursion.pdf
Recursion.pdf
 
Dynamic programing
Dynamic programingDynamic programing
Dynamic programing
 
Numerical differentation with c
Numerical differentation with cNumerical differentation with c
Numerical differentation with c
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
Introduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptxIntroduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptx
 
Partial compute function
Partial compute functionPartial compute function
Partial compute function
 
Exploring Tailrec Through Time Until Kotlin.pptx
Exploring Tailrec Through Time Until Kotlin.pptxExploring Tailrec Through Time Until Kotlin.pptx
Exploring Tailrec Through Time Until Kotlin.pptx
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
 
Python recursion
Python recursionPython recursion
Python recursion
 

Recently uploaded

Recently uploaded (20)

Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 

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