Quick actions

cmd+k|ctrl+k

Navigation

Languages

Fibonacci

Snippet info

Language

Csharp

Visibility

public

Author

linus.aspelin

Created

2017-12-15T15:25:43Z

Updated

2017-12-15T15:34:32Z

using System;
using System.Collections.Generic;
using System.Linq;

public static class Fibonacci
{
    public static IEnumerable<ulong> Enumerate()
    {
        var mem = new ulong[] { 1, 0 };
        for (byte b = 0; ; b ^= 1)
            yield return mem[b] += mem[1 - b];
    }
    public static ulong ValueAt(int n) => Enumerate().Skip(n - 1).First();
}

class MainClass {
    static void Main() {    
        //any number over n = 93 is too big for ulong; therefore, I use BigInteger in .NET Framework
        //I should implement my own BigInteger here, but I'm just too lazy for now ;)
        var n = 1;
        foreach(var value in Fibonacci.Enumerate().Take(93))
            Console.WriteLine($"{n++}: {value}");
    }
}
INFO