Lecture 5

9 an artificial example with rabbits rabbits never

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ems of the “same kind.” 9 An (artificial) example with rabbits. - Rabbits never die. - Rabbits always give birth to twins, male and female. - A couple of rabbits give birth to twins at the first day of every month, starting age 3 months. - We start with ADAM rabbit and EVE Rabbit. 11 FIBONACCI SEQUENCE: A sequence of numbers is defined as follows: The first number is 1. The second number is 1. Every other number is the sum of the two numbers before it. 1 1 2 3 5 8 13 21 34 55 89 8 int fib(int n) { if (n <= 2) return 1; else return fib(n-1) + fib(n-2); } The recursive implementation is, however, TERRIBLY inefficient, and is not recommended. fib(7) will call fib(3) 5 times! What a waste! 10 Month 1: Couples: 1 Adam, Eve 2: 1 Adam, Eve 3: 2 A&E and twins 1 4: 3 A&E twins 1, twins 2 5: 5 twins 1 have twins 4 6: 8 twins 2 have twins 7 Surprise, surprise,...
View Full Document

Ask a homework question - tutors are online