Advertisement
Guest User

Untitled

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