Advertisement
whitestarrr

4

Jan 31st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 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. int 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] = primes[1] = false;
  20. for (int p = 2; p <= n; p++)
  21. {
  22. if (primes[p])
  23. {
  24. for (int i = p * 2; i <= n; i += p)
  25. {
  26. primes[i] = false;
  27. }
  28. }
  29. }
  30. for (int i = 2; i <= n; i++)
  31. {
  32. if (primes[i] == true)
  33. {
  34. Console.Write(i + " ");
  35. }
  36.  
  37. }
  38.  
  39. }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement