Fibonacci Sequence
Learn to find patterns by working out the solution for the Fibonacci sequence.
Problem
Start with a pair of rabbits (one male and one female) born on January 1. Assume that all months are of equal length and that:
The first pair of rabbits reproduced two months after their own birth.
After two months of age, each pair produces a mixed pair (one male and one female), and then another mixed pair every month.
No rabbit dies.
Find out how many pairs will there be at the end of each month of the first year. Store the numbers in an array called array
.
The number of rabbit pairs are:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
Purpose
Analyze the problem and find out the pattern that matches the sequence
Variable assignment
Analyze
There is a pattern in the number of rabbit pairs. To find out the pattern, we need to work out the numbers in the first several months.
Month | Rabbit Pairs |
1 | 1 |
2 | 1 |
3 | 2 |
4 | 3 |
5 | 5 |
... | ... |
Fibonacci sequence
In the Fibonacci sequence, each new number is generated by adding the previous two numbers. By starting with ...