LusienGG

[C#] Sieve of Eratosthenes (Primes)

Apr 28th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.85 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 Practice
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             bool[] prime = new bool[n+1];
  15.             for (int i = 0; i < prime.Length; i++)
  16.             {
  17.                 prime[i] = true;
  18.             }
  19.             prime[0] = prime[1] = false;
  20.             for (int i = 2; i < prime.Length; i++)
  21.             {
  22.                 if (prime[i])
  23.                 {
  24.                     Console.Write(string.Join(", ",i));
  25.                     for (int j = i; j < prime.Length; j+=i)
  26.                     {
  27.                         prime[j] = false;
  28.                     }
  29.                 }
  30.             }
  31.                
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment