Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Lab_3
- {
- class Program
- {
- static void Main(string[] args)
- {
- int N = 500;
- /* bool[] arr = FindPrimesArr(N);
- for (int i = 0; i < N; i++)
- if (arr[i])
- Console.Write("{0} ", i);*/
- Console.WriteLine();
- var list = FindPrimesList(N);
- foreach (var i in list)
- Console.Write(i + " ");
- Console.ReadLine();
- }
- static bool[] FindPrimesArr(int N)
- {
- bool[] arr = new bool[N];
- for (int i = 0; i < N; i++)
- arr[i] = true;
- arr[0] = false; arr[1] = false;
- for (int i = 2; i * i < N; i++)
- if (arr[i])
- for (int j = i * i; j < N; j += i)
- arr[j] = false;
- return arr;
- }
- static LinkedList<int> FindPrimesList(int N)
- {
- LinkedList<int> list = new LinkedList<int>();
- for (int i = 2; i <= N; i++)
- list.AddLast(i);
- int count = 2;
- var node = list.First;
- bool isAllPrimes = true;
- while (true)
- {
- var next = node.Next;
- if (node.Value == count)
- {
- node = next == null ? list.First : next;
- continue;
- }
- else if (node.Value % count == 0)
- {
- list.Remove(node);
- isAllPrimes = false;
- }
- if (next == null)
- {
- node = list.First;
- if (isAllPrimes)
- break;
- }
- else
- node = next;
- }
- return list;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement