Have you ever heard about the Fibonacci Sequence? It often comes up in interviews, exams, or mathematics quizzes at that time you had questions in your mind like what, why, when, how this Fibonacci is related to us, or in other words why is the Fibonacci so important. Let’s learn it.

What is the Fibonacci Sequence?

Who discovered it?

Fibonacci is also known as Leonardo BonacciLeonardo of Pisa, or Leonardo Bigollo Pisano (‘Leonardo the Traveller from Pisa’), was an Italian mathematician from the Republic of Pisa, considered to be “the most talented Western mathematician of the Middle Ages”.

In his Young Age, he learned about the Hindu–Arabic numeral system in Bugia, Algeria. Fibonacci traveled around the Mediterranean coast, meeting with many merchants and learning about their systems of doing arithmetic. He soon realized the many advantages of the Hindu-Arabic system, which, unlike the Roman numerals used at the time, allowed easy calculation using a place-value system.

In 1202, he completed the Liber Abaci (Book of Abacus or The Book of Calculation), which popularized Hindu–Arabic numerals in Europe.

[Reference: https://en.wikipedia.org/wiki/Fibonacci]

How was the Fibonacci Sequence discovered?

Fibonacci Sequences or Fibonacci numbers is the answer to the hypothetical Rabbit Breeding question which was solved by Fibonacci (Leonardo Bigollo Pisano) in a Mathematician Competition in Pisa, Italy in 1225.

Question: Beginning with a male and female rabbit, how many pairs of rabbits could be born in a year? 

Answer: 233 pairs of rabbits at the end of a year.

Explanation :

  • Start: At the start, no rabbits are born, as the initial pair has not had time to become pregnant and give birth (0).
  • The first month: One pair of rabbits is born (1).
  • The second month: Again, one pair of rabbits is born as the new rabbits have not yet matured to bear offspring (1).
  • The third month: Two pairs of rabbits ready to reproduce, and one pair is not, so two pairs of rabbits are born (2).
  • The fourth month: Three pairs of rabbits reproduce and 2 pairs of rabbits are not ready, so three pairs of rabbits are born (3).
  • The fifth month: Five pairs of rabbits reproduce offspring and three are not ready to reproduce, so five pairs of rabbits are born (5).
  • And so on.

Fibonacci was not the first to know about the sequence. It was known in India hundreds of years before! It was discovered and described as “Virahanka” in Indian mathematic scriptures.

Fibonacci Sequence Theory

The Fibonacci sequence is a series of numbers starting from 0 and 1, where every number is the sum of the two numbers preceding it

Fibonacci Sequence Long List

0,1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, …

Mathematical equation of Fibonacci sequence

xn = xn-1 + xn-2.

where:

  • xn is term number “n”
  • xn−1 is the previous term (n−1)
  • xn−2 is the term before that (n−2)
1/12/13/25/38/513/821/1334/21
121.51.6661.61.6251.61531.61

Importance of the Fibonacci Series in Nature

When you divide any Fibonacci number (example: 5), with its preceding number (example: 3), the result is very close to 1.618, which is also called “Phi”. “Phi” is also believed to have been used in ancient Greece to denote the ratio of physical perfection.

0112358132134
  (0+1)(1+1)(1+2)(2+3)(3+5)(5+8)(8+13)(13+21)(…)
https://en.wikipedia.org/wiki/Fibonacci_number

The Fibonacci spiral: an approximation of the golden spiral created by drawing circular arcs connecting the opposite corners of squares in the Fibonacci tiling.

  • Fibonacci sequences are indeed found everywhere in nature/life. They’re useful at modeling the growth of animal populations, plant cell growth, snowflake shape, plant shape, cryptography, and of course computer science.

  • The petals of a flower, the seeds of fruits, rows of seeds on a sunflower, the spirals on a shell develop or add up to the Fibonacci numbers.

Use of Fibonacci Sequence in Computer science

  • The Fibonacci sequence is a good way to learn how to program a Recursive method

Fibonacci heap

  • A Fibonacci heap is a specific implementation of the heap data structure that makes use of Fibonacci numbers. Fibonacci heaps are used to implement the priority queue element in Dijkstra’s algorithm, giving the algorithm a very efficient running time. (reference: https://en.wikipedia.org/wiki/Fibonacci_heap)

TED Talk about Fibonacci Numbers:

Fibonacci Sequence and C#

Code Structure

Copy to Clipboard

Code Explanation

So what is happening in the code?

  • First, we create 4 integer variables:
    • f1 stores the first Fibonacci Number
    • f2 stores the second Fibonacci Number
    • f3 stores the next Fibonacci Number(3rd Fibonacci Number)
    • fibonacciLength stores Length of the Fibonacci Sequence in terms of Numbers that we want to print out.
  • Now read a value from the input stream and store it in the fibonacciLength variable.
  • Print the first 2 numbers of the Fibonacci Sequence which is already stored in f1 and f2.
  • Now we use a for loop to find the next Fibonacci number in every iteration from the 3rd number to the Length number provided by the user in the fibonacciLength variable.
  • In the Loop,
    • f3=f1+f2 means that the current Fibonacci number is the sum of the last 2 Fibonacci numbers.
    • print f3 (current Fibonacci number) in the console.
    • Now we want to find the next number (4th Fibonacci Number) so we need the sum of 2nd and 3rd Fibonacci Number so in the next line we write logic like
      • f1=f2;
      • f2=f3;

Int32. Parse(String) method is used to convert the string representation of a number to its 32-bit signed integer equivalent.

Console.ReadLine() reads the next line of characters from the standard input stream.

WriteLine() is a method of the Console class which is used to print the standard input, output, and error streams for console applications.

Code Output

Fibonacci Code Output

Summary

We learned about Fibonacci Number, History about Fibonacci Number, and How Fibonacci Number is connected with Nature and how we find n number of Fibonacci in C#.