Code Pyre

All Code Dies and Burns in Time

Fork me on GitHub

Fibonacci LINQ Sequence Generator Revisited

| Comments

t was pointed out that the sequence generator from the previous article could be written as an infinite sequence to allow .Skip(), .Take(), .ElementAt(), etc. Here is the infinite sequence version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static class Fibonacci
{
    public static IEnumerable<BigInteger> Generate()
    {
        yield return 1;
        yield return 1;

        BigInteger n1 = 1, n2 = 1;
        while(true)
        {
            BigInteger n3 = n1 + n2;
            n1 = n2;
            n2 = n3;
            yield return n3;
        }
    }
}

« WinDgb script when debugging drivers Build Systems »

Comments