Last active 1702408977

Fibonacci in C#

Program.cs Raw
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5public 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