fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. Func<int, IEnumerable<int>> nonPrimesUpTo = limit => Enumerable
  10. .Range(2, (int) (Math.Sqrt(limit)) - 1)
  11. .SelectMany(x => Enumerable
  12. .Range(2, short.MaxValue)
  13. .Select(y => x * y)
  14. .TakeWhile(p => p <= limit))
  15. .Distinct();
  16.  
  17. Func<int, IEnumerable<int>> sieve = limit => Enumerable
  18. .Range(2, limit - 1)
  19. .Except(nonPrimesUpTo(limit));
  20.  
  21. foreach (var prime in sieve(100)) {
  22. Console.WriteLine(prime);
  23. }
  24. }
  25. }
Success #stdin #stdout 0.06s 24160KB
stdin
Standard input is empty
stdout
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97