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 SieveOfEratosthenes
- {
- static void Main(string[] args)
- {
- int num = int.Parse(Console.ReadLine());
- bool[] primes = new bool[num + 1];
- for (int i = 0; i < primes.Length; i++)
- {
- primes[i] = true;
- }
- primes[0] = primes[1] = false;
- for (int i = 2; i < num; i++)
- {
- if (primes[i])
- {
- FillPrimes(primes, i);
- }
- }
- for (int i = 2; i < primes.Length; i++)
- {
- if (primes[i])
- {
- Console.Write(i + " ");
- }
- }
- Console.WriteLine();
- }
- private static void FillPrimes(bool[] primes, int step)
- {
- for (int i = step* 2; i < primes.Length; i += step)
- {
- primes[i] = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement