Last active
March 10, 2021 10:42
-
-
Save ArisAgnew/b3f322fb711bbf60ac40e1fedeb70b97 to your computer and use it in GitHub Desktop.
Fibonacci IEnumerable C#
This file contains hidden or 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
static void Main() | |
{ | |
foreach (var num in Fibonacci(7)) | |
{ | |
Console.WriteLine(num); | |
} | |
} | |
protected static IEnumerable<uint> Fibonacci(uint number) | |
{ | |
uint a = 0, b = 1, f = 1; | |
if (number >= 1) | |
{ | |
yield return 0; | |
yield return 1; | |
} | |
for (var i = 2; i <= number; ++i) | |
{ | |
f = a + b; | |
a = b; | |
b = f; | |
yield return f; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment