A newborn pair of rabbits has a new pair each month from month 3 on, and so does each new pair. No rabbit dies. How many pairs are there in month month? Write rabbits(month). Months 1 and 2 have 1 pair; then each month is the sum of the two before: 1, 1, 2, 3, 5, 8… This is the Fibonacci sequence.
rabbits(1)→1rabbits(3)→2rabbits(6)→8long long rabbits(int month)long long rabbits(int month) {
long long f1 = 1, f2 = 1;
for (int m = 3; m <= month; m++) {
// your code here: work out next month, then move f1 and f2 one step on
}
return f2;
}
Run tries the examples. When they look right, Submit.