Kodewick
Start learning →
Starter←→0 / 50

Fibonacci rabbits

#012Starterf1 + f25 min

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.

Examples
rabbits(1)→1
rabbits(3)→2
rabbits(6)→8
The functionlong long rabbits(int month)
main.c
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;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.