C# - Iterate over fibonacci numbers

By , 7/31/2015
(0 ratings)
This small structure contains all to iterate over the list of Fibonacci numbers.
The optional parameter is the zero based index of the first number in the sequence. The code behind that calculation based on this snippet: http://dotnet-snippets.de/snippet/fibonacci-zahl-mittels-matrix-berechnen/3866

Required Namespaces
System.Collections.Generic;
using System.Numerics

Additional required Assembly
System.Numerics

Sample Appliction
static void Main(string[] args)
{
foreach (var fib in Fibonacci.Enumerate())
{
Console.WriteLine(fib);
Thread.Sleep(500);
}
}
Translate to VB
/// <summary>
/// Provides methods to iterate over fibonacci numbers.
/// </summary>
public struct Fibonacci
{
    #region static

    /// <summary>
    /// Iterates over the fibonacci numbers starting at 1 or at the given zero based index.
    /// </summary>
    /// <param name="start">The zero based index of the fibonacci number with which to start the iteration.</param>
    /// <returns>An <see cref="System.Collections.IEnumerable{T}"/> with all fibonacci numbers.</returns>
    public static IEnumerable<Fibonacci> Enumerate(uint start = 0)
    {
        BigInteger a, b;
        FibonacciAtPosition(start, out a, out b);

        yield return new Fibonacci(start++, a);
        while (true)
        {
            yield return new Fibonacci(start++, b);
            var tmp = a;
            a = b;
            b = tmp + b;
        }
    }

    /// <summary>
    /// Calculates two consecutive fibonacci numbers. The smaller one is at the given index.
    /// </summary>
    /// <seealso href="http://dotnet-snippets.de/snippet/fibonacci-zahl-mittels-matrix-berechnen/3866"/>
    static void FibonacciAtPosition(uint position, out BigInteger outA, out BigInteger outB)
    {
        ++position;//position is zero based, the algorithm is not
        BigInteger[,] fib = { { 1, 1 }, { 1, 0 } };
        BigInteger[,] blu = { { 1, 0 }, { 0, 1 } };

        while (position > 0)
        {
            if (position % 2 != 0)
                blu = ProcessMatrix(blu, fib);

            fib = ProcessMatrix(fib, fib);

            position /= 2;
        }
        outA = blu[0, 1];
        outB = blu[0, 0];
    }

    static BigInteger[,] ProcessMatrix(BigInteger[,] blu, BigInteger[,] fib)
    {
        return new BigInteger[,]
    {
        {
            blu[0, 0] * fib[0, 0] + blu[0, 1] * fib[1, 0],
            blu[0, 0] * fib[0, 1] + blu[0, 1] * fib[1, 1],
        },
        {
            blu[1, 0] * fib[0, 0] + blu[1, 1] * fib[1, 0],
            blu[1, 0] * fib[0, 1] + blu[1, 1] * fib[1, 1],
        }
    };
    }

    #endregion

    #region instance

    /// <summary>
    /// Gets the zero based index of the fibonacci number.
    /// </summary>
    public uint Position { get; private set; }
    /// <summary>
    /// Gets the fibonacci number.
    /// </summary>
    public BigInteger Number { get; private set; }

    private Fibonacci(uint position, BigInteger number)
        : this()
    {
        this.Position = position;
        this.Number = number;
    }

    /// <summary>
    /// Gets a string representation of the fibonacci number.
    /// </summary>
    public override string ToString()
    {
        return string.Format("{0}: {1}", this.Position, this.Number);
    }

    #endregion
}
Tagged with Fibonacci, Iterator, LINQ.

Comments

 

Log in, to comment!