Created
December 6, 2012 15:28
-
-
Save flagbug/4225321 to your computer and use it in GitHub Desktop.
Infinite Fibonacci sequence generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; }); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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