7. Recursions - 7 RECURSIONS Rocky K C Chang Objectives To...

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

7. RECURSIONS Rocky K. C. Chang October 12, 2015
Image of page 1

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

Objectives To be able to design recursive solutions to solve "simple" problems. To understand the relationship between recursion and mathematical induction. To understand that recursion is a postponement of work. To know how to design recursive algorithms systematically.
Image of page 2
WHAT IS RECURSION?
Image of page 3

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

Example 1: Computation of n! Standard method: fact(n) = 1 * 2 * … * n Recursive method: fact(n) = n * fact(n-1) fact(n-1) = (n-1) * fact(n-2) fact(2) = 2 * fact(1) fact(1) = 1 * fact(0) fact(0) = 1
Image of page 4
Implement a recursive function to compute n!. EXERCISE 7.1
Image of page 5

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

Example 2: Birthday cake cutting Question: how many pieces of a circular cake as a result of n cuts through the center? Write down your answer here.
Image of page 6
Example 2 Recursive approach:
Image of page 7

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

Example 3: Fibonacci numbers The rabbit population problem: Start out with a male-female pair of rabbits. It takes two months for each pair to be reproductive. Each pair reproduces another male-female pair in each subsequent month. How many pairs of male-female rabbits after n months if no rabbits die during this period? Standard method: 1 st month: 1 2 nd month: 1 3 rd month: 2 4 th month: 3
Image of page 8
Example 3 Recursive method: Just prior to the start of n th month, there are fib(n-1) pairs.
Image of page 9

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

Image of page 10
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