Advertisement
Filkolev

Sieve Of Eratosthenes

May 27th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.69 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement