Filkolev

Sieve Of Eratosthenes

May 27th, 2015
44
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Linq;
  3.  
  4. public class SieveOfEratosthenes
  5. {
  6.     public static void Main()
  7.     {
  8.         int n = int.Parse(Console.ReadLine());
  9.  
  10.         int currentPrime = 2;
  11.  
  12.         int[] numbers = Enumerable.Range(0, n + 1).ToArray();
  13.  
  14.         while (currentPrime < n)
  15.         {
  16.             for (int i = currentPrime * 2; i <= n; i += currentPrime)
  17.             {
  18.                 numbers[i] = 0;
  19.             }
  20.  
  21.             currentPrime++;
  22.             while (numbers[currentPrime] < 2 && currentPrime < n)
  23.             {
  24.                 currentPrime++;
  25.             }
  26.         }
  27.  
  28.         Console.WriteLine(string.Join(", ", numbers.Where(number => number >= 2)));
  29.     }
  30. }
RAW Paste Data