The Fibonacci sequence of numbers was introduced by Italian mathematician Fibonacci in twelfth century.
· Suppose that we start with a pair of rabbits, one male and one female.
· Rabbits mature for breeding in two months.
· Also assume that no rabbits ever die, and every pair of rabbits produce a pair of offspring (male + female) each month after maturity.
The n-th Fibonacci number in the sequence is the number of pairs at n-th month. The first two months there is only one pair of rabbits. In month 3 the first pair matures and produces another pair, so there are two pairs. In month 4 the first pair produces another pair of offsprings, so there are 3 pairs of rabbits. In month five the first offspring pair is also mature so, there are two additional pairs of offsprings, i.e. 5 pairs. The number of pairs you have each month will generate Fibonacci sequence.
public int fib(int n)
{
if (n <= 2)
return 1;
else
return fib(n‑1) + fib(n‑2);
}
.
|
|