Advertisement
Guest User

Untitled

a guest
Oct 7th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 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 Sieve_of_Eratosthenes
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int n = int.Parse(Console.ReadLine());
  14. bool[] isPrime = new bool[n+1];
  15.  
  16. for (int i = 0; i <= n; i++)
  17. {
  18. isPrime[i] = true;
  19. }
  20. isPrime[0] = false;
  21. isPrime[1] = false;
  22. for (int i = 2; i <= n; i++)
  23. {
  24. if (isPrime[i])
  25. {
  26. for (int y = i; y <= n; y += i)
  27. {
  28. if (y % i == 0 && y != i)
  29. {
  30. isPrime[y] = false;
  31. }
  32. }
  33. }
  34. }
  35.  
  36. for (int i = 2; i <= n; i++)
  37. {
  38. if (isPrime[i] == true)
  39. {
  40. Console.Write(i + " ");
  41. }
  42. }
  43. Console.WriteLine();
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement