Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Created August 9, 2012 08:17
Show Gist options
  • Save masaru-b-cl/3302274 to your computer and use it in GitHub Desktop.
Save masaru-b-cl/3302274 to your computer and use it in GitHub Desktop.
菊池さん考案のフィボナッチ数列なIEnumerable<int>を返す関数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FibonacciSequence
{
class Program
{
static void Main(string[] args)
{
var i = 0;
foreach (var fib in Fib().Take(11))
{
Console.WriteLine(i + ":" + fib);
i++;
}
}
private static IEnumerable<int> Fib()
{
var a = 0;
var b = 1;
yield return a;
yield return b;
for (; ; )
{
a = a + b;
yield return a;
b = a + b;
yield return b;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment