Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 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 Lab_3
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int N = 500;
  14. /* bool[] arr = FindPrimesArr(N);
  15. for (int i = 0; i < N; i++)
  16. if (arr[i])
  17. Console.Write("{0} ", i);*/
  18. Console.WriteLine();
  19. var list = FindPrimesList(N);
  20. foreach (var i in list)
  21. Console.Write(i + " ");
  22.  
  23. Console.ReadLine();
  24. }
  25.  
  26. static bool[] FindPrimesArr(int N)
  27. {
  28. bool[] arr = new bool[N];
  29. for (int i = 0; i < N; i++)
  30. arr[i] = true;
  31. arr[0] = false; arr[1] = false;
  32. for (int i = 2; i * i < N; i++)
  33. if (arr[i])
  34. for (int j = i * i; j < N; j += i)
  35. arr[j] = false;
  36. return arr;
  37. }
  38.  
  39. static LinkedList<int> FindPrimesList(int N)
  40. {
  41. LinkedList<int> list = new LinkedList<int>();
  42. for (int i = 2; i <= N; i++)
  43. list.AddLast(i);
  44. int count = 2;
  45. var node = list.First;
  46. bool isAllPrimes = true;
  47. while (true)
  48. {
  49. var next = node.Next;
  50. if (node.Value == count)
  51. {
  52. node = next == null ? list.First : next;
  53. continue;
  54. }
  55.  
  56. else if (node.Value % count == 0)
  57. {
  58. list.Remove(node);
  59. isAllPrimes = false;
  60. }
  61. if (next == null)
  62. {
  63. node = list.First;
  64. if (isAllPrimes)
  65. break;
  66. }
  67. else
  68. node = next;
  69.  
  70. }
  71. return list;
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement