# euler / csharp / e10.cs

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 /* The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. Answer: */ using System; using System.Collections; class e10 { static IEnumerable longs() { long i = 2; while (true) { yield return i++; } } static IEnumerable filter(long p, IEnumerable nums) { foreach (long i in nums) { if ((i % p) != 0) { yield return i; } } } static long head(IEnumerable items) { foreach (long i in items) { return i; } // We'll never get here, just make the compiler happy return -1; } static IEnumerable primes() { IEnumerable stream = longs(); while (true) { long p = head(stream); stream = filter(p, stream); yield return p; } } static void Main() { long sum = 0; foreach (long p in primes()) { if (p > 2000000) { break; } Console.WriteLine(p); sum += p; } Console.WriteLine(sum); } }