Advertisement
Guest User

Fibonacci

a guest
Aug 16th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.80 KB | None | 0 0
  1.         public static class Fibonacci
  2.         {      
  3.             public static IEnumerable<int> GetNumbers()
  4.             {
  5.                 yield return 0;
  6.  
  7.                 int current = 1;
  8.                 int previous = 0;          
  9.  
  10.                 while (true)
  11.                 {
  12.                     yield return current;              
  13.                     current += previous;
  14.                     previous = current - previous;
  15.                 }                
  16.             }
  17.         }
  18.  
  19.         class Program
  20.         {
  21.             static void Main(string[] args)
  22.             {
  23.                 int count = Fibonacci.GetNumbers().Where(x => x % 2 == 1).TakeWhile(x => x < 4000000).Count();
  24.                 Console.WriteLine(count);                      
  25.             }      
  26.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement