Advertisement
Guest User

4.SieveOfEratosthenes

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