Skip to content

Instantly share code, notes, and snippets.

@ArisAgnew
Last active March 10, 2021 10:42
Show Gist options
  • Save ArisAgnew/b3f322fb711bbf60ac40e1fedeb70b97 to your computer and use it in GitHub Desktop.
Save ArisAgnew/b3f322fb711bbf60ac40e1fedeb70b97 to your computer and use it in GitHub Desktop.
Fibonacci IEnumerable C#
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