Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 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 SieveOfEratosthenes
  10. {
  11. static void Main(string[] args)
  12. {
  13. int num = int.Parse(Console.ReadLine());
  14. bool[] primes = new bool[num + 1];
  15.  
  16. for (int i = 0; i < primes.Length; i++)
  17. {
  18. primes[i] = true;
  19. }
  20.  
  21. primes[0] = primes[1] = false;
  22.  
  23. for (int i = 2; i < num; i++)
  24. {
  25. if (primes[i])
  26. {
  27. FillPrimes(primes, i);
  28. }
  29. }
  30.  
  31. for (int i = 2; i < primes.Length; i++)
  32. {
  33. if (primes[i])
  34. {
  35. Console.Write(i + " ");
  36. }
  37. }
  38. Console.WriteLine();
  39.  
  40. }
  41.  
  42. private static void FillPrimes(bool[] primes, int step)
  43. {
  44. for (int i = step* 2; i < primes.Length; i += step)
  45. {
  46. primes[i] = false;
  47. }
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement