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:
- Queen: One special female bee in the colony is called the Queen and is the only female that produces eggs.
- Workers: The remaining female bees are workers in the colony and produce no eggs.
- 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.
- using System;
- namespace FibonacciSeries
- {
- class Program
- {
- static int FibonacciSeries(int n)
- {
- int firstnumber = 0, secondnumber = 1, result = 0;
-
- if (n == 0) return 0;
- if (n == 1) return 1;
-
-
- for (int i = 2; i <= n; i++)
- {
- result = firstnumber + secondnumber;
- firstnumber = secondnumber;
- secondnumber = result;
- }
-
- return result;
- }
-
- static void Main(string[] args)
- {
- Console.Write("Enter the length of the Fibonacci Series: ");
- int length = Convert.ToInt32(Console.ReadLine());
-
- for (int i = 0; i < length; i++)
- {
- Console.Write("{0} ", FibonacciSeries(i));
- }
- Console.ReadKey();
- }
- }
- }
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.
- using System;
- namespace FibonacciSeries
- {
- class Program
- {
- public static int FibonacciSeries(int n)
- {
- if (n == 0) return 0;
- if (n == 1) return 1;
- return FibonacciSeries(n - 1) + FibonacciSeries(n - 2);
- }
- public static void Main(string[] args)
- {
- Console.Write("Enter the length of the Fibonacci Series: ");
- int length = Convert.ToInt32(Console.ReadLine());
- for (int i = 0; i < length; i++)
- {
- Console.Write("{0} ", FibonacciSeries(i));
- }
- Console.ReadKey();
- }
- }
- }
There is another way to use the recursive method:
- using System;
- namespace FibonacciSeries
- {
- class Program
- {
- public static void FibonacciRecursive
- (
- int firstnumber,
- int secondnumber,
- int counter,
- int length
- )
- {
- if (counter <= length)
- {
- Console.Write("{0} ", firstnumber);
- FibonacciRecursive(secondnumber, firstnumber + secondnumber, counter + 1, length);
- }
- }
-
- public static void Main(string[] args)
- {
- Console.Write("Enter the length of the Fibonacci Series: ");
- int length = Convert.ToInt32(Console.ReadLine());
- FibonacciRecursive(0, 1, 1, length);
- Console.ReadKey();
- }
- }
- }
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.
- using System;
- namespace FibonacciSeries
- {
- class Program
- {
- public static int NthFibonacciNumber(int n)
- {
- if ((n == 0) || (n == 1))
- {
- return n;
- }
- else
- {
- return (NthFibonacciNumber(n - 1) + NthFibonacciNumber(n - 2));
- }
- }
-
- public static void Main(string[] args)
- {
- Console.Write("Enter the nth number of the Fibonacci Series: ");
- int number = Convert.ToInt32(Console.ReadLine());
- number = number - 1;
-
-
- Console.Write(NthFibonacciNumber(number));
- Console.ReadKey();
- }
- }
- }
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.