# euler / csharp / e7.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 61``` ```/* By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? Answer: */ using System; using System.Collections; class e7 { 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() { int n = 0; foreach (long p in primes()) { n++; if ((n % 100) == 0) { Console.WriteLine(n); } if (n == 10001) { Console.WriteLine(p); break; } } } } ```