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());
- bool[] arrCheck = new bool[n + 1];
- string primes = string.Empty;
- for (int i = 0; i <=n; i++)
- {
- if (i == 0 || i == 1)
- {
- arrCheck[i] = false;
- }
- else
- {
- arrCheck[i] = true;
- }
- }
- var square = Math.Sqrt(n);
- for (int i = 2; i <= square; i++)
- {
- if (arrCheck[i])
- {
- int p=2;
- while(p*i<=n)
- {
- arrCheck[p*i]=false;
- p++;
- }
- }
- }
- for (int i = 0; i <=n; i++)
- {
- if(arrCheck[i])
- {
- primes+=i+" ";
- }
- }
- Console.WriteLine(primes);
- }
- }
- }
Add Comment
Please, Sign In to add comment