Fibonacci numbers
Lesson 4 of 20 in Coddy's Mathematical Riddles course.
Each new term in the Fibonacci sequence is generated by adding the previous two terms.
a[n+2]=a[n]+a[n+1]By starting with 1 and 2, the first 10 terms below 100 will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89.
The Fibonacci sequence appears in the book Liber Abaci (The Book of Calculation, 1202) by Fibonacci where it is used to calculate the growth of rabbit populations. Fibonacci considers the growth of an idealized (biologically unrealistic) rabbit population, assuming that: a newly born breeding pair of rabbits are put in a field; each breeding pair mates at the age of one month, and at the end of their second month they always produce another pair of rabbits; and rabbits never die, but continue breeding forever. Fibonacci posed the puzzle: how many pairs will there be in one year?
At the end of the nth month, the number of pairs of rabbits is equal to the number of mature pairs (that is, the number of pairs in month n – 2) plus the number of pairs alive last month (month n – 1). The number in the nth month is the nth Fibonacci number. [Wikipedia, the free encyclopedia]
Challenge
EasyWrite python code calcFibonacciNums that get a natural number, <i>N</i>, and return the number of terms of the Fibonacci sequence, starting with 1 and 2, whose values do not exceed <i>N</i>.
Try it yourself
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "solution.h"
int main() {
int n;
if (scanf("%d", &n) != 1) n = 0;
int r = calcFibonacciNums(n);
printf("%d\n", r);
return 0;
}