SlideShare una empresa de Scribd logo
1 de 35
Intro to Recursion in
Programming with Fractals
@gurpalsingh204
Presentation version of blog post on
Singhcodes.wordpress.com
What is recursion?
● Recursion simply is applying a procedure, breaking it into smaller
parts, and applying the same to the smaaller parts.
● Example: Fibonacci series: F(n) = F(n-1)+F(n-2)
● Usually one has to get some experience before getting a good feel of
the concept.
● But if you could see what happens under the hood you could get a
better understanding more quickly.
● Fractals are perfect for this purpose besides being beautiful.
What are fractals?
● A Fractal, basically, is a repeating pattern.
● Each smaller part of the fractal is identical to
the bigger part.
● If you zoom into an ideal fractal, you couldn’t
tell how much you have zoomed in.
An Example of fractals
● The mandelbrot set is a famous fractal
Let's make some fractals!
The Koch Line
● We will begin with the simplest fractal : The Koch Line
● The rule to make Koch line:
1.Draw (an imaginary) straight line.
2.Split it into three equal parts.
3.Replace the middle part with an equilateral triangle without a
base
4.Apply the same procedure to all the new straight
lines(recursion).
5.Finally draw the lines.
Defining iterations
● Usually it is not feasible Step 4, in the last slide,
a lot of times.
● We need to stop after a finite number of times.
● Each execution of step 1-3 is an iteration.
● The number of times we apply the rule is called
the number of iterations.
The Koch Line
● Next we see the result of executing the steps.
● I used simple C graphics to make the fractals.
● The code is available on GitHub.
The Koch Line
● One Iteration:
The Koch Line
● Two Iterations:
The Koch Line
● Three Iterations:
The Koch Line
● Four Iterations:
The Koch Snowflake
● The Koch Line procedure could be used to
make a Koch Snowflake
● To make a Koch Snowflake:
1.Make an equlateral trinangle
2.Apply Koch Line procedure to all the sides of the
triangle
● Let's see what the resulting fractal looks like.
The Koch Snowflake
The Koch Snowflake
● In different colours
One More Example:
The Sierpinski Arrowhead Curve
The Sierpinski Arrowhead Curve
● Following are the steps to make a sierpinski
arrowhead curve
1. Draw an imaginary line. Think of it as a base of an
equilateral triangle.
2. Move up (while drawing) one side. Stop halfway.
3. Move parallel to base till you reach the other side.
4. Move down the side to the base line.
5. Apply same procedure to all lines( Recursion ). The
triangles on slanting lines must point inwards. The
triangle on the horizontal line must point outwards.
The Sierpinski Arrowhead Curve
● One Iteraton:
The Sierpinski Arrowhead Curve
● Two Iteratons:
The Sierpinski Arrowhead Curve
● Three Iteratons:
The Sierpinski Arrowhead Curve
● Four and Five iterations skipped..
The Sierpinski Arrowhead Curve
● Six iterations:
The Sierpinski Arrowhead Curve
● In different colours:
Conclusions
Things to Note About Fractals
● The final structure is completely different form
what we started with.
● Each smaller part is completely identical to the
bigger part.
Recursion in Simple Language
● Recursion mainly consists of following two
steps:
1.Apply some procedure to a thing.
2.Start from Step 1 for all smaller parts.
● In practice, we stop when we meet a base
case.
Fractals are everywhere!
Romanesco Broccoli
Copper Crystals
Applications of recursion in
Programming
Quick Sort
algorithm quicksort(A, lo, hi) is
if lo < hi then
p := partition(A, lo, hi)
quicksort(A, lo, p - 1)
quicksort(A, p + 1, hi)
Recursive
Calls
Merge Sort
function merge_sort(list m)
// Base case. A list of zero or one elements is sorted, by definition.
if m is empty then
return m
// Recursive case. First, divide the list into equal-sized sublists
// consisting of the even and odd-indexed elements.
//omitted
// RECURSIVELY sort both sublists.
left := merge_sort(left)
right := merge_sort(right)
// Then merge the now-sorted sublists.
return merge(left, right)
Tower of Hanoi
function hanoi is:
input: integer n, such that n >= 1
1. if n is 1 then return 1
2. return [2 * [call hanoi(n-1)] + 1]
end hanoi
● Interestingly, graphical representation of tower of
hanoi is similar to that of sierpinski arrowhead curve
Recursive
Call
Useful Resources
● Original blog post
● Code Used to make fractals
● Snowflakes
● Fractals in Nature
● Wolfram Fractals Reference App
● Think Python book
Thank You
● Check out my blog for interesting new posts:
singhcodes.wordpress.com
● Follow me on twitter:
@gurpalsingh204

Más contenido relacionado

Destacado

Niagen-Nectar7-Brochure0116_Athlete
Niagen-Nectar7-Brochure0116_AthleteNiagen-Nectar7-Brochure0116_Athlete
Niagen-Nectar7-Brochure0116_Athlete
David D'Arcangelo
 
Gaby El Khoury - 01-June-15
Gaby El Khoury - 01-June-15Gaby El Khoury - 01-June-15
Gaby El Khoury - 01-June-15
Gaby El Khoury
 

Destacado (17)

Vaikuttaminen on joukkuelaji - myös somessa
Vaikuttaminen on joukkuelaji - myös somessaVaikuttaminen on joukkuelaji - myös somessa
Vaikuttaminen on joukkuelaji - myös somessa
 
Curiosidades de decimo b
Curiosidades de decimo bCuriosidades de decimo b
Curiosidades de decimo b
 
Presentation 1 choiniere
Presentation 1 choinierePresentation 1 choiniere
Presentation 1 choiniere
 
Niagen-Nectar7-Brochure0116_Athlete
Niagen-Nectar7-Brochure0116_AthleteNiagen-Nectar7-Brochure0116_Athlete
Niagen-Nectar7-Brochure0116_Athlete
 
Diapo1eva
Diapo1evaDiapo1eva
Diapo1eva
 
Gaby El Khoury - 01-June-15
Gaby El Khoury - 01-June-15Gaby El Khoury - 01-June-15
Gaby El Khoury - 01-June-15
 
Rpp revisi 2016 sejarah wajib xii rpp diva pendidikan
Rpp revisi 2016 sejarah wajib xii  rpp diva pendidikanRpp revisi 2016 sejarah wajib xii  rpp diva pendidikan
Rpp revisi 2016 sejarah wajib xii rpp diva pendidikan
 
Rpp revisi 2016 ekonomi x rpp diva pendidikan
Rpp revisi 2016 ekonomi x  rpp diva pendidikanRpp revisi 2016 ekonomi x  rpp diva pendidikan
Rpp revisi 2016 ekonomi x rpp diva pendidikan
 
La sarcoïdose une maladie qu’on peut confondre avec la tuberculose
La sarcoïdose une maladie qu’on peut confondre avec la tuberculoseLa sarcoïdose une maladie qu’on peut confondre avec la tuberculose
La sarcoïdose une maladie qu’on peut confondre avec la tuberculose
 
How To Open A Restaurant In New York
How To Open A Restaurant In New YorkHow To Open A Restaurant In New York
How To Open A Restaurant In New York
 
Rpp revisi 2016 sejarah wajib x rpp diva pendidikan
Rpp revisi 2016 sejarah wajib x  rpp diva pendidikanRpp revisi 2016 sejarah wajib x  rpp diva pendidikan
Rpp revisi 2016 sejarah wajib x rpp diva pendidikan
 
Rpp revisi 2016 ekonomi xi rpp diva pendidikan
Rpp revisi 2016 ekonomi xi  rpp diva pendidikanRpp revisi 2016 ekonomi xi  rpp diva pendidikan
Rpp revisi 2016 ekonomi xi rpp diva pendidikan
 
GOOD AND EVIL
GOOD AND EVILGOOD AND EVIL
GOOD AND EVIL
 
5.30 final brunch
5.30 final brunch5.30 final brunch
5.30 final brunch
 
March 1st Treasure Emporium with Riley and Friends
March 1st Treasure Emporium with Riley and FriendsMarch 1st Treasure Emporium with Riley and Friends
March 1st Treasure Emporium with Riley and Friends
 
Ampul Spring 2016 Final
Ampul Spring 2016 FinalAmpul Spring 2016 Final
Ampul Spring 2016 Final
 
Rpp revisi 2016 biologi xi rpp diva pendidikan
Rpp revisi 2016 biologi xi  rpp diva pendidikanRpp revisi 2016 biologi xi  rpp diva pendidikan
Rpp revisi 2016 biologi xi rpp diva pendidikan
 

Similar a Awesome Introduction to Recursion in Programming with Fractals

Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
Sure Interview
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
monicafrancis71118
 
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
 
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdfQ-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Michpice
 
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingSkiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programming
zukun
 

Similar a Awesome Introduction to Recursion in Programming with Fractals (20)

How invariants help writing loops
How invariants help writing loopsHow invariants help writing loops
How invariants help writing loops
 
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
 
Integration
IntegrationIntegration
Integration
 
Interview questions slide deck
Interview questions slide deckInterview questions slide deck
Interview questions slide deck
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
 
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
 
Exponents
ExponentsExponents
Exponents
 
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
 
8 algorithms rubik's cube
8 algorithms rubik's cube8 algorithms rubik's cube
8 algorithms rubik's cube
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
 
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
 
Writing algorithms
Writing algorithmsWriting algorithms
Writing algorithms
 
Course Design Best Practices
Course Design Best PracticesCourse Design Best Practices
Course Design Best Practices
 
Derivatives and it’s simple applications
Derivatives and it’s simple applicationsDerivatives and it’s simple applications
Derivatives and it’s simple applications
 
Chapter 9 divide and conquer handouts with notes
Chapter 9   divide and conquer handouts with notesChapter 9   divide and conquer handouts with notes
Chapter 9 divide and conquer handouts with notes
 
L06
L06L06
L06
 
Init() Lesson 3
Init() Lesson 3Init() Lesson 3
Init() Lesson 3
 
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdfQ-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
 
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingSkiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programming
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Último (20)

Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

Awesome Introduction to Recursion in Programming with Fractals

  • 1. Intro to Recursion in Programming with Fractals @gurpalsingh204 Presentation version of blog post on Singhcodes.wordpress.com
  • 2. What is recursion? ● Recursion simply is applying a procedure, breaking it into smaller parts, and applying the same to the smaaller parts. ● Example: Fibonacci series: F(n) = F(n-1)+F(n-2) ● Usually one has to get some experience before getting a good feel of the concept. ● But if you could see what happens under the hood you could get a better understanding more quickly. ● Fractals are perfect for this purpose besides being beautiful.
  • 3. What are fractals? ● A Fractal, basically, is a repeating pattern. ● Each smaller part of the fractal is identical to the bigger part. ● If you zoom into an ideal fractal, you couldn’t tell how much you have zoomed in.
  • 4. An Example of fractals ● The mandelbrot set is a famous fractal
  • 5. Let's make some fractals!
  • 6. The Koch Line ● We will begin with the simplest fractal : The Koch Line ● The rule to make Koch line: 1.Draw (an imaginary) straight line. 2.Split it into three equal parts. 3.Replace the middle part with an equilateral triangle without a base 4.Apply the same procedure to all the new straight lines(recursion). 5.Finally draw the lines.
  • 7. Defining iterations ● Usually it is not feasible Step 4, in the last slide, a lot of times. ● We need to stop after a finite number of times. ● Each execution of step 1-3 is an iteration. ● The number of times we apply the rule is called the number of iterations.
  • 8. The Koch Line ● Next we see the result of executing the steps. ● I used simple C graphics to make the fractals. ● The code is available on GitHub.
  • 9. The Koch Line ● One Iteration:
  • 10. The Koch Line ● Two Iterations:
  • 11. The Koch Line ● Three Iterations:
  • 12. The Koch Line ● Four Iterations:
  • 13. The Koch Snowflake ● The Koch Line procedure could be used to make a Koch Snowflake ● To make a Koch Snowflake: 1.Make an equlateral trinangle 2.Apply Koch Line procedure to all the sides of the triangle ● Let's see what the resulting fractal looks like.
  • 15. The Koch Snowflake ● In different colours
  • 16. One More Example: The Sierpinski Arrowhead Curve
  • 17. The Sierpinski Arrowhead Curve ● Following are the steps to make a sierpinski arrowhead curve 1. Draw an imaginary line. Think of it as a base of an equilateral triangle. 2. Move up (while drawing) one side. Stop halfway. 3. Move parallel to base till you reach the other side. 4. Move down the side to the base line. 5. Apply same procedure to all lines( Recursion ). The triangles on slanting lines must point inwards. The triangle on the horizontal line must point outwards.
  • 18. The Sierpinski Arrowhead Curve ● One Iteraton:
  • 19. The Sierpinski Arrowhead Curve ● Two Iteratons:
  • 20. The Sierpinski Arrowhead Curve ● Three Iteratons:
  • 21. The Sierpinski Arrowhead Curve ● Four and Five iterations skipped..
  • 22. The Sierpinski Arrowhead Curve ● Six iterations:
  • 23. The Sierpinski Arrowhead Curve ● In different colours:
  • 25. Things to Note About Fractals ● The final structure is completely different form what we started with. ● Each smaller part is completely identical to the bigger part.
  • 26. Recursion in Simple Language ● Recursion mainly consists of following two steps: 1.Apply some procedure to a thing. 2.Start from Step 1 for all smaller parts. ● In practice, we stop when we meet a base case.
  • 30. Applications of recursion in Programming
  • 31. Quick Sort algorithm quicksort(A, lo, hi) is if lo < hi then p := partition(A, lo, hi) quicksort(A, lo, p - 1) quicksort(A, p + 1, hi) Recursive Calls
  • 32. Merge Sort function merge_sort(list m) // Base case. A list of zero or one elements is sorted, by definition. if m is empty then return m // Recursive case. First, divide the list into equal-sized sublists // consisting of the even and odd-indexed elements. //omitted // RECURSIVELY sort both sublists. left := merge_sort(left) right := merge_sort(right) // Then merge the now-sorted sublists. return merge(left, right)
  • 33. Tower of Hanoi function hanoi is: input: integer n, such that n >= 1 1. if n is 1 then return 1 2. return [2 * [call hanoi(n-1)] + 1] end hanoi ● Interestingly, graphical representation of tower of hanoi is similar to that of sierpinski arrowhead curve Recursive Call
  • 34. Useful Resources ● Original blog post ● Code Used to make fractals ● Snowflakes ● Fractals in Nature ● Wolfram Fractals Reference App ● Think Python book
  • 35. Thank You ● Check out my blog for interesting new posts: singhcodes.wordpress.com ● Follow me on twitter: @gurpalsingh204