{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS2_14_Recursion - CS2 Module 14 Category CS Concepts Topic...

Info icon This preview shows pages 1–26. Sign up to view the full content.

View Full Document Right Arrow Icon
CS2 Module 14 Category: CS Concepts Topic: Recursion Objectives Head and Tail Recursion
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS 2 Introduction to Object Oriented Programming Module 14 CS Concepts Recursion
Image of page 2
Recursion Remember Recursion? A programming technique whereby a method calls itself. Still relies on three things: Check for termination Move closer to solution Make Recursive call
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Let’s say you place two rabbits in a hutch. What’s going to happen? Recursion
Image of page 4
If it takes two months for rabbits to reach maturity, in two months you’ll have one productive pair and one (brand new) non-productive pair. (This assumes all rabbits live up to their reputation.) Two Months Later... original rabbits new rabbits
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
1 month old 0 months old The next month, you get another pair . . . The rabbits keep at it.
Image of page 6
Suppose What if: The rabbits always had two offspring, always male and female; Rabbits always reached maturity in two months; No rabbit dies. How many pairs of rabbits do you have in a year? 1 1 1 1 2 2 2 2 3 3 3 3
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Hare Raising Story Start End Month 5 End Month 1 End Month 2 End Month 3 End Month 4 = one m/f pair KEY
Image of page 8
Pairs Pairs of Rabbits of Rabbits 1 2 3 4 5 6 7 8 9 10 11 12 Month Productive Non-Productive Total 0 1 1 2 3 5 8 13 21 34 55 89 0 1 1 2 3 5 8 13 21 34 55 1 1 1 2 3 5 8 13 21 34 55 89 144 See a pattern yet?
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Let’s Take Another Example Instead of rabbits, let’s use geometry. Draw a square of size 1. Rotating 90 degrees, add to it a square of size 1. Rotating 90 degrees again, add a square of size 2. Again, rotate and add a square of size 3, and so on. Keep this up for the sequence we noted in the table: 1, 1, 2, 3, 5, 8, 13, 21, . . . , What do you see?
Image of page 10
1
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
1
Image of page 12
2
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
3
Image of page 14
5
Image of page 15

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
8
Image of page 16
13
Image of page 17

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
21
Image of page 18
1 1 2 3 5 8 13 21
Image of page 19

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Does this look familiar?
Image of page 20
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern