Revisiting Algorithms: Fibonacci Sequence

I am starting new posts that visit many of the familiar algorithms that we have already learnt in the past. I will use C# as the primary language to represent these algorithms. To start, let us start with a very simple example, Fibonacci Series. 0,1,1,2,3,5,8,13,21,..etc are typically called Fibonacci numbers. These numbers were well known in ancient India. In short, each number is a sum of the previous two numbers with the seed values being 0, 1. The following is the C# code that outputs a series of Fibonacci numbers with input being the number of Fibonacci numbers to generate.

   1:  public static int[] GenerateFibonacciNumbers(int n)
   2:  {
   3:      int[] arr = new int[n];
   4:      arr[0] = 0;
   5:      arr[1] = 1;
   6:      for (int i = 2; i < n; i++)
   7:      {
   8:          arr[i] = arr[i - 1] + arr[i - 2];
   9:      }
  10:      return arr;
  11:  }

Leave a Reply