Advertisement
NonaG

SieveOfEratosthenes

Feb 23rd, 2017
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SieveOfEratosthenes
  8. {
  9. class SieveOfEratosthenes
  10. {
  11. static void Main(string[] args)
  12. {
  13. var n = int.Parse(Console.ReadLine());
  14. bool[] primes = new bool[n+1];
  15. for (int i = 0; i <= n; i++)
  16. {
  17. primes[i] = true;
  18. }
  19. primes[0] = false;
  20. primes[1] = false;
  21. for (int p = 0; p < n; p++)
  22. {
  23. if (primes[p])
  24. {
  25. for (int j = 2; j*p <= n; j++)
  26. {
  27. primes[j*p] = false;
  28. }
  29. }
  30. }
  31. for (int i = 0; i < primes.Length; i++)
  32. {
  33. if (primes[i])
  34. {
  35. Console.Write(i+" ");
  36. }
  37. }
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement