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 _04.Sieve_of_Eratosthenes
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int[] arr = new int[n + 1];
- bool[] arrCheck = new bool[n + 1];
- string primes = string.Empty;
- for (int i = 0; i < arr.Length; i++)
- {
- if (i == 0 || i == 1)
- {
- arr[i] = i;
- arrCheck[i] = false;
- }
- else
- {
- arr[i] = i;
- arrCheck[i] = true;
- }
- }
- for (int i = 0; i < arr.Length; i++)
- {
- if (arrCheck[i])
- {
- primes += $"{arr[i]}";
- primes += " ";
- for (int j = i + 1; j < arr.Length; j++)
- {
- if (arr[j] % i == 0 && arrCheck[j] == true)
- {
- arrCheck[j] = false;
- }
- }
- }
- }
- Console.WriteLine(primes);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement