Skip to content

Instantly share code, notes, and snippets.

@flagbug
Created December 6, 2012 15:28
Show Gist options
  • Save flagbug/4225321 to your computer and use it in GitHub Desktop.
Save flagbug/4225321 to your computer and use it in GitHub Desktop.
Infinite Fibonacci sequence generator
using MoreLinq;
using System.Collections.Generic;
public IEnumerable<ulong> GenerateFibonacci()
{
ulong previous1 = 1;
ulong previous2 = 0;
return MoreEnumerable
.Generate(0UL, i => previous1 + previous2)
.Pipe(i => { previous2 = previous1; previous1 = i; });
}
using System.Collections.Generic;
public IEnumerable<ulong> GenerateFibonacci()
{
ulong previous1 = 0;
ulong previous2 = 1;
ulong current = 0;
while(true)
{
yield return current;
current = previous1 + previous2;
previous2 = previous1;
previous1 = current;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment