SoFunction
Updated on 2025-03-07

C# uses iterative method to implement Fibnaci sequences

This article describes the method of C# using iterative method to implement Fibnaci sequences. Share it for your reference. The specific analysis is as follows:

The following is a basic introduction to the Fibnaci sequence:

Here Fibnaci represents the array name and n represents the index.
For example: Fibnaci cardinality sequence: 1, 1, 2, 3, 5, 8...
When n<=2: Fibnaci(n)=1
When n>2: Fibnaci(n)=Fibnaci(n-1)+Fibnaci(n-2)

We can use recursion or iteration methods to perform algorithm programming, and here we introduce iterative methods.
Other non-recursive methods can also be referred to as follows.

public List<int> BaseNumbers = new List<int> { 1, 1, 2, 3, 5, 8 };
public int GetFibnaceNumber(List<int> baseNumbers, int len)
{
  if (len <= 2)
  {
 return 1;
  }
  else if ((len - 1) <= )
  {
 len = len - 1;
 return BaseNumbers[len - 1] + BaseNumbers[len - 2];
  }
  else
  {
 int BaseMaxIndex = ;
 (BaseNumbers[BaseMaxIndex - 1] + BaseNumbers[BaseMaxIndex - 2]);
 return GetFibnaceNumber(BaseNumbers, len);
  }
}

I hope this article will be helpful to everyone's C# programming.