Leonardo Fibonacci was a talented Italian mathematician. He is best known for a number sequence named after him known as the Fibonacci Numbers.
These numbers take the following pattern:
0, 1, 1, 2, 3, 5, 8, 13, 21 …
The series is formed by adding two numbers in the series to generate the third.
The Rabbit Puzzle that Fibonacci investigated in the year 1202 is a great example to introduce you to the significance of the Fibonacci number series.
Fibonacci’s Rabbits Puzzle was about calculating the number of rabbits if they mate and breed in ideal circumstances. As such, the question was: ‘How many pairs of rabbits will be produced in a year, beginning with a single pair, if in every month each pair bears a new pair which becomes productive from the second month on?’
To investigate the matter, the following was assumed:
- Begin with a newly-born pair of rabbits, one male, one female. Rabbits can mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits.
- The rabbits never die.
- The female produces one male and one female every month.
The following Java code produces numbers following Fibonacci pattern. Feel free to edit the value of ‘stopAt’ to extend the number of numbers being produced by the code. Have fun!
public class FibonacciSeries { public static void fibCode() { int n1 = 0; int n2 = 1; int n3; int stopAt = 12; System.out.println(n1); System.out.println(n2); for(int i=0;i<stopAt;i++) { n3 = n1+n2; System.out.println(n3); n1 = n2; n2 = n3; } } public static void main(String [] args) { fibCode(); } }
