SHARE
TWEET

Sieve of Eratosthenes - find Prime Numbers

plamen27 Aug 24th, 2016 702 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _3_4_Exercize_Prime_Numbers
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             bool[] array = new bool[n + 1];
  15.             for (int i = 0; i <= n; i++)
  16.             {
  17.                 array[i] = true;
  18.             }
  19.             array[0] = false; array[1] = false;
  20.             for (int i = 0; i < array.Length; i++)
  21.             {
  22.                 if (array[i] == true)
  23.                 {
  24.                     for (int a = 2; (a * i) <= n; a++)            
  25.                     {
  26.                         array[a * i] = false;
  27.                     }
  28.                 }
  29.             }
  30.             for (int j = 2; j <= n; j++)
  31.             {
  32.                 if (array[j] == true)
  33.                 { Console.Write(j + " "); }
  34.             }
  35.             Console.WriteLine();
  36.         }
  37.     }
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top