Advertisement
YavorGrancharov

Sieve_of_Eratosthenes

Oct 13th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Sieve_of_Eratosthenes
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             long n = long.Parse(Console.ReadLine());
  10.             long[] num = new long[n + 1];
  11.             long count = 2;
  12.             for (int i = 2; i <= n; i++)
  13.             {
  14.                 num[i] = count;
  15.                 count++;
  16.             }
  17.             for (int i = 2; (i * i) <= n; i++)
  18.             {
  19.                 for (int j = (i * i); j <= n; j+=i)
  20.                 {
  21.                     num[j] = 0;
  22.                 }
  23.             }
  24.             for (int i = 2; i <= n; i++)
  25.             {
  26.                 if (num[i] != 0)
  27.                 {
  28.                     Console.Write(num[i] + " ");
  29.                 }
  30.             }
  31.             Console.WriteLine();
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement