TeMePyT

Untitled

Jun 1st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 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 _04.Sieve_of_Eratosthenes
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             bool[] arrCheck = new bool[n + 1];
  15.             string primes = string.Empty;
  16.             for (int i = 0; i <=n; i++)
  17.             {
  18.                 if (i == 0 || i == 1)
  19.                 {
  20.                     arrCheck[i] = false;
  21.                 }
  22.                 else
  23.                 {
  24.                     arrCheck[i] = true;
  25.                 }
  26.  
  27.             }
  28.            var square = Math.Sqrt(n);
  29.             for (int i = 2; i <= square; i++)
  30.             {
  31.                 if (arrCheck[i])
  32.                 {
  33.                   int p=2;
  34.                     while(p*i<=n)
  35.                     {
  36.                         arrCheck[p*i]=false;
  37.                         p++;
  38.                     }
  39.                 }
  40.             }
  41.             for (int i = 0; i <=n; i++)
  42.             {
  43.                 if(arrCheck[i])
  44.                 {
  45.                     primes+=i+" ";
  46.                 }
  47.             }
  48.             Console.WriteLine(primes);
  49.         }
  50.     }
  51. }
Add Comment
Please, Sign In to add comment