The Fibonacci Numbers

Introduction

I'm sure every one of you have attempted to generate Fibonacci numbers once in a while during college labs or during an interview. Most of us just learn the procedure to generate the sequence in C, C++ or C# and thought that the deal is done. Have you ever thought of why we generate this sequence? What is the significance of this sequence? This blog helps you to explore the various possibilities of Fibonacci numbers and also provides the simple logic to generate the sequence.

Fibonacci Numbers

Definition

The Fibonacci Numbers is a sequence of numbers in the following order: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34... The next number is found by adding up the two numbers before it. The formula for calculating these numbers is:

F(n) = F(n-1) + F(n-2)

where:

  • F(n) is the term number.
  • F(n-1) is the previous term (n-1).
  • F(n-2) is the term before that (n-2). 
By definition, the first two numbers in the Fibonacci Sequence are either 0 and 1, or 1 and 1, depending on the chosen starting point of the sequence and each subsequent number is the sum of the previous two numbers.

History

The Fibonacci numbers or Fibonacci sequence is a series of numbers named after a famous mathematician Leonardo Pisano (popularly known as Fibonacci), although he did not discover this sequence but used it as an example in his book Liber Abaci, which means "The Book of Calculations". The Fibonacci series was originally known in Indian Mathematics hundreds of years before he used it in his book.

Fibonacci Numbers in Real life Scenarios

The Fibonacci Numbers play a significant role in real life scenarios. Many of the numbers in the Fibonacci sequence can be related to the things that we see around us. These numbers are well represented in Honeybees. Honeybees, usually, lives in a colony called a Hive and they have a very unusual Family Tree. In a colony of honeybees, they are divided into three categories: 
  1. Queen: One special female bee in the colony is called the Queen and is the only female that produces eggs.

  2. Workers: The remaining female bees are workers in the colony and produce no eggs.

  3. Drones: The male bees do not work and are called Drone Bees.

Male bees are produced by the queen's unfertilized eggs, so they only have a mother but no father. All the female bees are produced when the queen has mated with a male bee and so have both the parents, mother as well as father. Most of the female bees cannot produce eggs and become worker bees but some of the lucky ones grow into queens and go off to start a new colony. So, female bees have two parents, a male and a female whereas male bees have just one parent, a female.

If we follow the family tree of Honeybees, it represents the Fibonacci sequence perfectly. A male bee has 1 parent, a female. He has 2 grandparents, a male and a female since his mother had two parents. He has 3 great-grandparents: his grandmother has two parents but his grandfather had only one."

Number of

Parents

Grand Parents

Great Grand-Parents

Gt Gt Grand-Parents

Gt Gt Gt Grand-Parents

Male Bee 1 2 3 5 8

Female Bee

2

3 5 8 13

Fibonacci numbers also appear in plants and flowers. Many plants show the Fibonacci numbers in the arrangement of their leaves around the stem. Leaves are arranged in such a way that each leaf is exposed to a significant amount of sunlight and is not suppressed by the top leaves of the plant.

Here is the link of a TED Talk about Fibonacci Numbers:

Fibonacci Series in C#

The Fibonacci numbers are a fascinating sequence of numbers. We can generate the sequence in various ways. Let's use some special cases to generate the Fibonacci sequence.

Case 1 (Iterative Approach): This approach is the simplest and quickest way to generate the sequence of Fibonacci Numbers.

  1. using System;   
  2. namespace FibonacciSeries  
  3. {  
  4.     class Program  
  5.     {  
  6.         static int FibonacciSeries(int n)  
  7.         {  
  8.             int firstnumber = 0, secondnumber = 1, result = 0;  
  9.    
  10.             if (n == 0) return 0; //To return the first Fibonacci number   
  11.             if (n == 1) return 1; //To return the second Fibonacci number   
  12.    
  13.    
  14.             for (int i = 2; i <= n; i++)  
  15.             {  
  16.                 result = firstnumber + secondnumber;  
  17.                 firstnumber = secondnumber;  
  18.                 secondnumber = result;  
  19.             }  
  20.    
  21.             return result;  
  22.         }  
  23.    
  24.         static void Main(string[] args)  
  25.         {  
  26.             Console.Write("Enter the length of the Fibonacci Series: ");  
  27.             int length = Convert.ToInt32(Console.ReadLine());  
  28.    
  29.             for (int i = 0; i < length; i++)  
  30.             {  
  31.                 Console.Write("{0} ", FibonacciSeries(i));  
  32.             }  
  33.             Console.ReadKey();  
  34.         }  
  35.     }  
  36. }  
Case 2 (Recursive Approach): In this approach, we use recursion to process the sequence of numbers. We need to pass the length of the Fibonacci Series to the recursive method and then it iterates continuously until it reaches the goal.
  1. using System;  
  2. namespace FibonacciSeries  
  3. {  
  4.       class Program  
  5.       {  
  6.             public static int FibonacciSeries(int n)  
  7.             {  
  8.                   if (n == 0) return 0; //To return the first Fibonacci number   
  9.                   if (n == 1) return 1; //To return the second Fibonacci number   
  10.                   return FibonacciSeries(n - 1) + FibonacciSeries(n - 2);  
  11.             }  
  12.             public static void Main(string[] args)  
  13.             {  
  14.                   Console.Write("Enter the length of the Fibonacci Series: ");  
  15.                   int length = Convert.ToInt32(Console.ReadLine());  
  16.                   for (int i = 0; i < length; i++)  
  17.                   {  
  18.                         Console.Write("{0} ", FibonacciSeries(i));  
  19.                   }  
  20.                   Console.ReadKey();  
  21.             }  
  22.       }  
  23. }  
There is another way to use the recursive method:
  1. using System;  
  2. namespace FibonacciSeries  
  3. {  
  4.     class Program  
  5.     {  
  6.         public static void FibonacciRecursive  
  7.        (  
  8.         int firstnumber,  
  9.         int secondnumber,  
  10.         int counter,  
  11.         int length  
  12.        )  
  13.         {  
  14.             if (counter <= length)  
  15.             {  
  16.                 Console.Write("{0} ", firstnumber);  
  17.                 FibonacciRecursive(secondnumber, firstnumber + secondnumber, counter + 1, length);  
  18.             }  
  19.         }  
  20.    
  21.         public static void Main(string[] args)  
  22.         {  
  23.             Console.Write("Enter the length of the Fibonacci Series: ");  
  24.             int length = Convert.ToInt32(Console.ReadLine());  
  25.             FibonacciRecursive(0, 1, 1, length);  
  26.             Console.ReadKey();  
  27.         }  
  28.     }  
  29. }   
Case 3 (nth Fibonacci Number): We can also find the nth number of the Fibonacci sequence using Recursion. One point must be note down that the user input is decrement by 1 because we are starting the series with 0.
  1. using System;  
  2. namespace FibonacciSeries  
  3. {  
  4.     class Program  
  5.     {  
  6.         public static int NthFibonacciNumber(int n)  
  7.         {  
  8.             if ((n == 0) || (n == 1))  
  9.             {  
  10.                 return n;  
  11.             }  
  12.             else  
  13.             {  
  14.                 return (NthFibonacciNumber(n - 1) + NthFibonacciNumber(n - 2));  
  15.             }  
  16.         }  
  17.    
  18.         public static void Main(string[] args)  
  19.         {  
  20.             Console.Write("Enter the nth number of the Fibonacci Series: ");  
  21.             int number = Convert.ToInt32(Console.ReadLine());  
  22.             number = number - 1;         
  23.             //We have to decrement the length because the series starts with 0  
  24.    
  25.             Console.Write(NthFibonacciNumber(number));  
  26.             Console.ReadKey();  
  27.         }  
  28.     }  
  29. }  
Conclusion

In this article, I've explained the significance of Fibonacci Numbers and also provides several routinies to determine the series. I hope this article helps you to understand Fibonacci Numbers. Your feedback and constructive criticism is always appreciated, keep it coming. Until then, try to put a ding in the Universe.

Up Next
    Ebook Download
    View all
    Learn
    View all