COMPUTING
7. Recursions

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

• Notes
• 33

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

7. RECURSIONS Rocky K. C. Chang October 12, 2015

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.
WHAT IS RECURSION?

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
Implement a recursive function to compute n!. EXERCISE 7.1

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.
Example 2 Recursive approach:

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
Example 3 Recursive method: Just prior to the start of n th month, there are fib(n-1) pairs.

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

This is the end of the preview. Sign up to access the rest of the document.
• Fall '16
• Rocky Chiang

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern