SlideShare a Scribd company logo
1 of 8
Recursion
Chapter 17
17
What is recursion?
Recursion occurs when a method calls
itself, either directly or indirectly.
Used to solve difficult, repetitive
problems
17
Factorial Example
N! (N Factorial)
N! = N * (N – 1)! for N >= 0 and 0! = 1
5! = 5 * 4! or 5 * 4 * 3 * 2 * 1 * 1 = 120
4! = 4 * 3! or 4 * 3 * 2 * 1 * 1 = 24
3! = 3 * 2! or 3 * 2 * 1 * 1 = 6
2! = 2 * 1! or 2 * 1 * 1 = 2
1! = 1 * 0! or 1 * 1 = 1
0! = 1
17
Calculate Factorials Using Recursion
The Java solution:
private int doFactorial(int N)
{
if ( N == 0 )
{
return 1;
}
return N * doFactorial(N – 1);
}
17
Towers of Hanoi
Three towers hold rings stacked in order
from largest to smallest (bottom to top)
Player can only move one ring at a time
Larger rings cannot be stacked on top of
smaller ring
Player must move stack to another tower
17
The Towers of Hanoi Algorithm
Classic problem can be solved with
recursion.
Move N–1 rings to storage tower
Move Nth ring to destination tower
Move N–1 rings to destination tower
17
Towers of Hanoi Solution
Final graphical solution includes the files:
TowerRing.java
Tower.java
TowerMove.java
ThreeTowers.java
TowersOfHanoi.java
TowerPanel.java
17
Graphical Towers of Hanoi
Applet in progress

More Related Content

Viewers also liked (7)

Chapter 18
Chapter 18Chapter 18
Chapter 18
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Enterprise Skills
Enterprise SkillsEnterprise Skills
Enterprise Skills
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Key enterprise skills
Key enterprise skillsKey enterprise skills
Key enterprise skills
 
Chapter 11
Chapter 11Chapter 11
Chapter 11
 

More from Graham Royce (20)

Chapter 13
Chapter 13Chapter 13
Chapter 13
 
Chapter 10
Chapter 10Chapter 10
Chapter 10
 
Chapter 09
Chapter 09Chapter 09
Chapter 09
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
 
Chapter 07
Chapter 07Chapter 07
Chapter 07
 
Chapter 06
Chapter 06Chapter 06
Chapter 06
 
Chapter 05
Chapter 05Chapter 05
Chapter 05
 
Chapter 04
Chapter 04Chapter 04
Chapter 04
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
Chapter 02
Chapter 02Chapter 02
Chapter 02
 
Chapter 01
Chapter 01Chapter 01
Chapter 01
 
13 java in oracle
13 java in oracle13 java in oracle
13 java in oracle
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
My 3 min pitch pack
My 3 min pitch packMy 3 min pitch pack
My 3 min pitch pack
 
Things to do
Things to doThings to do
Things to do
 
House in order
House in orderHouse in order
House in order
 
Blank 10 all you need
Blank 10 all you needBlank 10 all you need
Blank 10 all you need
 
Goals
GoalsGoals
Goals
 
Idea's1 only
Idea's1 onlyIdea's1 only
Idea's1 only
 
New frontiers motivation
New frontiers motivationNew frontiers motivation
New frontiers motivation
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

Chapter 17

  • 2. 17 What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive problems
  • 3. 17 Factorial Example N! (N Factorial) N! = N * (N – 1)! for N >= 0 and 0! = 1 5! = 5 * 4! or 5 * 4 * 3 * 2 * 1 * 1 = 120 4! = 4 * 3! or 4 * 3 * 2 * 1 * 1 = 24 3! = 3 * 2! or 3 * 2 * 1 * 1 = 6 2! = 2 * 1! or 2 * 1 * 1 = 2 1! = 1 * 0! or 1 * 1 = 1 0! = 1
  • 4. 17 Calculate Factorials Using Recursion The Java solution: private int doFactorial(int N) { if ( N == 0 ) { return 1; } return N * doFactorial(N – 1); }
  • 5. 17 Towers of Hanoi Three towers hold rings stacked in order from largest to smallest (bottom to top) Player can only move one ring at a time Larger rings cannot be stacked on top of smaller ring Player must move stack to another tower
  • 6. 17 The Towers of Hanoi Algorithm Classic problem can be solved with recursion. Move N–1 rings to storage tower Move Nth ring to destination tower Move N–1 rings to destination tower
  • 7. 17 Towers of Hanoi Solution Final graphical solution includes the files: TowerRing.java Tower.java TowerMove.java ThreeTowers.java TowersOfHanoi.java TowerPanel.java
  • 8. 17 Graphical Towers of Hanoi Applet in progress