際際滷

際際滷Share a Scribd company logo
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 couldnt
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

More Related Content

Viewers also liked (17)

Vaikuttaminen on joukkuelaji - my旦s somessa
Vaikuttaminen on joukkuelaji - my旦s somessaVaikuttaminen on joukkuelaji - my旦s somessa
Vaikuttaminen on joukkuelaji - my旦s somessa
Tero Era
Curiosidades de decimo bCuriosidades de decimo b
Curiosidades de decimo b
Celeste Quinga
Presentation 1 choiniere
Presentation 1 choinierePresentation 1 choiniere
Presentation 1 choiniere
Annie Choiniere
Niagen-Nectar7-Brochure0116_Athlete
Niagen-Nectar7-Brochure0116_AthleteNiagen-Nectar7-Brochure0116_Athlete
Niagen-Nectar7-Brochure0116_Athlete
David D'Arcangelo
Diapo1evaDiapo1eva
Diapo1eva
Paola Carolina
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
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
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
Diva Pendidikan
La sarco誰dose une maladie quon peut confondre avec la tuberculoseLa sarco誰dose une maladie quon peut confondre avec la tuberculose
La sarco誰dose une maladie quon peut confondre avec la tuberculose
Khadija Moussayer
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
PTN Commerce
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
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
Diva Pendidikan
GOOD AND EVIL
GOOD AND EVILGOOD AND EVIL
GOOD AND EVIL
Aishwarya Menon
5.30 final brunch
5.30 final brunch5.30 final brunch
5.30 final brunch
Britney Stanley-Wyatt
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
Britney Stanley-Wyatt
Ampul Spring 2016 Final
Ampul Spring 2016 FinalAmpul Spring 2016 Final
Ampul Spring 2016 Final
Hayley Stratton
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
Diva Pendidikan
Vaikuttaminen on joukkuelaji - my旦s somessa
Vaikuttaminen on joukkuelaji - my旦s somessaVaikuttaminen on joukkuelaji - my旦s somessa
Vaikuttaminen on joukkuelaji - my旦s somessa
Tero Era
Curiosidades de decimo bCuriosidades de decimo b
Curiosidades de decimo b
Celeste Quinga
Presentation 1 choiniere
Presentation 1 choinierePresentation 1 choiniere
Presentation 1 choiniere
Annie Choiniere
Niagen-Nectar7-Brochure0116_Athlete
Niagen-Nectar7-Brochure0116_AthleteNiagen-Nectar7-Brochure0116_Athlete
Niagen-Nectar7-Brochure0116_Athlete
David D'Arcangelo
Diapo1evaDiapo1eva
Diapo1eva
Paola Carolina
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
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
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
Diva Pendidikan
La sarco誰dose une maladie quon peut confondre avec la tuberculoseLa sarco誰dose une maladie quon peut confondre avec la tuberculose
La sarco誰dose une maladie quon peut confondre avec la tuberculose
Khadija Moussayer
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
PTN Commerce
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
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
Diva Pendidikan
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
Britney Stanley-Wyatt
Ampul Spring 2016 Final
Ampul Spring 2016 FinalAmpul Spring 2016 Final
Ampul Spring 2016 Final
Hayley Stratton
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
Diva Pendidikan

Similar to 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
nextbuild
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
Andres Mendez-Vazquez
Integration
IntegrationIntegration
Integration
AbhayPandey117
Interview questions slide deck
Interview questions slide deckInterview questions slide deck
Interview questions slide deck
MikeBegley
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
Sure Interview
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
Professor Lili Saghafi
Exponents
ExponentsExponents
Exponents
Tankiso Tale
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
Herman Peeren
8 algorithms rubik's cube
8 algorithms rubik's cube8 algorithms rubik's cube
8 algorithms rubik's cube
Ravi Kiran
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
Writing algorithms
Writing algorithmsWriting algorithms
Writing algorithms
Krishna Chaytaniah
Course Design Best Practices
Course Design Best PracticesCourse Design Best Practices
Course Design Best Practices
Keitaro Matsuoka
Derivatives and its simple applications
Derivatives and its simple applicationsDerivatives and its simple applications
Derivatives and its simple applications
Rutuja Gholap
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
mailund
L06
L06L06
L06
Saurav Rahman
Init() Lesson 3
Init() Lesson 3Init() Lesson 3
Init() Lesson 3
UCLA Association of Computing Machinery
Lecture-12-CS345A-2023 of Design and Analysis
Lecture-12-CS345A-2023 of Design and AnalysisLecture-12-CS345A-2023 of Design and Analysis
Lecture-12-CS345A-2023 of Design and Analysis
ssuser9183b6
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
How invariants help writing loops
How invariants help writing loopsHow invariants help writing loops
How invariants help writing loops
nextbuild
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
Andres Mendez-Vazquez
Interview questions slide deck
Interview questions slide deckInterview questions slide deck
Interview questions slide deck
MikeBegley
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
Sure Interview
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
Professor Lili Saghafi
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
Herman Peeren
8 algorithms rubik's cube
8 algorithms rubik's cube8 algorithms rubik's cube
8 algorithms rubik's cube
Ravi Kiran
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
Course Design Best Practices
Course Design Best PracticesCourse Design Best Practices
Course Design Best Practices
Keitaro Matsuoka
Derivatives and its simple applications
Derivatives and its simple applicationsDerivatives and its simple applications
Derivatives and its simple applications
Rutuja Gholap
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
mailund
Lecture-12-CS345A-2023 of Design and Analysis
Lecture-12-CS345A-2023 of Design and AnalysisLecture-12-CS345A-2023 of Design and Analysis
Lecture-12-CS345A-2023 of Design and Analysis
ssuser9183b6
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

Recently uploaded (20)

Research & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptxResearch & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptx
Dr. Sarita Anand
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
sandynavergas1
Rass MELAI : an Internet MELA Quiz Finals - El Dorado 2025
Rass MELAI : an Internet MELA Quiz Finals - El Dorado 2025Rass MELAI : an Internet MELA Quiz Finals - El Dorado 2025
Rass MELAI : an Internet MELA Quiz Finals - El Dorado 2025
Conquiztadors- the Quiz Society of Sri Venkateswara College
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
Association for Project Management
cervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdfcervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdf
SamarHosni3
How to attach file using upload button Odoo 18
How to attach file using upload button Odoo 18How to attach file using upload button Odoo 18
How to attach file using upload button Odoo 18
Celine George
How to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 EmployeeHow to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 Employee
Celine George
PUBH1000 Module 3: Public Health Systems
PUBH1000 Module 3: Public Health SystemsPUBH1000 Module 3: Public Health Systems
PUBH1000 Module 3: Public Health Systems
Jonathan Hallett
QuickBooks Desktop to QuickBooks Online How to Make the Move
QuickBooks Desktop to QuickBooks Online  How to Make the MoveQuickBooks Desktop to QuickBooks Online  How to Make the Move
QuickBooks Desktop to QuickBooks Online How to Make the Move
TechSoup
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptxTLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
RizaBedayo
Year 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptxYear 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptx
mansk2
Kaun TALHA quiz Finals -- El Dorado 2025
Kaun TALHA quiz Finals -- El Dorado 2025Kaun TALHA quiz Finals -- El Dorado 2025
Kaun TALHA quiz Finals -- El Dorado 2025
Conquiztadors- the Quiz Society of Sri Venkateswara College
Reordering Rules in Odoo 17 Inventory - Odoo 際際滷s
Reordering Rules in Odoo 17 Inventory - Odoo 際際滷sReordering Rules in Odoo 17 Inventory - Odoo 際際滷s
Reordering Rules in Odoo 17 Inventory - Odoo 際際滷s
Celine George
The Battle of Belgrade Road: A WW1 Street Renaming Saga by Amir Dotan
The Battle of Belgrade Road: A WW1 Street Renaming Saga by Amir DotanThe Battle of Belgrade Road: A WW1 Street Renaming Saga by Amir Dotan
The Battle of Belgrade Road: A WW1 Street Renaming Saga by Amir Dotan
History of Stoke Newington
Mate, a short story by Kate Grenvile.pptx
Mate, a short story by Kate Grenvile.pptxMate, a short story by Kate Grenvile.pptx
Mate, a short story by Kate Grenvile.pptx
Liny Jenifer
Adventure Activities Final By H R Gohil Sir
Adventure Activities Final By H R Gohil SirAdventure Activities Final By H R Gohil Sir
Adventure Activities Final By H R Gohil Sir
GUJARATCOMMERCECOLLE
TPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategyTPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategy
Henry Tapper
Computer Application in Business (commerce)
Computer Application in Business (commerce)Computer Application in Business (commerce)
Computer Application in Business (commerce)
Sudar Sudar
How to Setup WhatsApp in Odoo 17 - Odoo 際際滷s
How to Setup WhatsApp in Odoo 17 - Odoo 際際滷sHow to Setup WhatsApp in Odoo 17 - Odoo 際際滷s
How to Setup WhatsApp in Odoo 17 - Odoo 際際滷s
Celine George
The Broccoli Dog's inner voice (look A)
The Broccoli Dog's inner voice  (look A)The Broccoli Dog's inner voice  (look A)
The Broccoli Dog's inner voice (look A)
merasan
Research & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptxResearch & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptx
Dr. Sarita Anand
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
sandynavergas1
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
Association for Project Management
cervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdfcervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdf
SamarHosni3
How to attach file using upload button Odoo 18
How to attach file using upload button Odoo 18How to attach file using upload button Odoo 18
How to attach file using upload button Odoo 18
Celine George
How to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 EmployeeHow to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 Employee
Celine George
PUBH1000 Module 3: Public Health Systems
PUBH1000 Module 3: Public Health SystemsPUBH1000 Module 3: Public Health Systems
PUBH1000 Module 3: Public Health Systems
Jonathan Hallett
QuickBooks Desktop to QuickBooks Online How to Make the Move
QuickBooks Desktop to QuickBooks Online  How to Make the MoveQuickBooks Desktop to QuickBooks Online  How to Make the Move
QuickBooks Desktop to QuickBooks Online How to Make the Move
TechSoup
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptxTLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
RizaBedayo
Year 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptxYear 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptx
mansk2
Reordering Rules in Odoo 17 Inventory - Odoo 際際滷s
Reordering Rules in Odoo 17 Inventory - Odoo 際際滷sReordering Rules in Odoo 17 Inventory - Odoo 際際滷s
Reordering Rules in Odoo 17 Inventory - Odoo 際際滷s
Celine George
The Battle of Belgrade Road: A WW1 Street Renaming Saga by Amir Dotan
The Battle of Belgrade Road: A WW1 Street Renaming Saga by Amir DotanThe Battle of Belgrade Road: A WW1 Street Renaming Saga by Amir Dotan
The Battle of Belgrade Road: A WW1 Street Renaming Saga by Amir Dotan
History of Stoke Newington
Mate, a short story by Kate Grenvile.pptx
Mate, a short story by Kate Grenvile.pptxMate, a short story by Kate Grenvile.pptx
Mate, a short story by Kate Grenvile.pptx
Liny Jenifer
Adventure Activities Final By H R Gohil Sir
Adventure Activities Final By H R Gohil SirAdventure Activities Final By H R Gohil Sir
Adventure Activities Final By H R Gohil Sir
GUJARATCOMMERCECOLLE
TPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategyTPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategy
Henry Tapper
Computer Application in Business (commerce)
Computer Application in Business (commerce)Computer Application in Business (commerce)
Computer Application in Business (commerce)
Sudar Sudar
How to Setup WhatsApp in Odoo 17 - Odoo 際際滷s
How to Setup WhatsApp in Odoo 17 - Odoo 際際滷sHow to Setup WhatsApp in Odoo 17 - Odoo 際際滷s
How to Setup WhatsApp in Odoo 17 - Odoo 際際滷s
Celine George
The Broccoli Dog's inner voice (look A)
The Broccoli Dog's inner voice  (look A)The Broccoli Dog's inner voice  (look A)
The Broccoli Dog's inner voice (look A)
merasan

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 couldnt 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