Program.cs
· 399 B · C#
Raw
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
foreach (var i in Fibonacci().Take(20))
{
Console.WriteLine(i);
}
}
private static IEnumerable<int> Fibonacci()
{
int current = 1, next = 1;
while (true)
{
yield return current;
next = current + (current = next);
}
}
}
1 | using System; |
2 | using System.Collections.Generic; |
3 | using System.Linq; |
4 | |
5 | public class Program |
6 | { |
7 | public static void Main() |
8 | { |
9 | foreach (var i in Fibonacci().Take(20)) |
10 | { |
11 | Console.WriteLine(i); |
12 | } |
13 | } |
14 | |
15 | private static IEnumerable<int> Fibonacci() |
16 | { |
17 | int current = 1, next = 1; |
18 | |
19 | while (true) |
20 | { |
21 | yield return current; |
22 | next = current + (current = next); |
23 | } |
24 | } |
25 | } |
26 |