SHARE
TWEET

14. SieveOfEro

Teodor92 Jan 6th, 2013 931 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a program that finds all prime numbers
  2.  * in the range [1...10 000 000]. Use the sieve
  3.  * of Eratosthenes algorithm (find it in Wikipedia).
  4.  */
  5.  
  6. using System;
  7.  
  8. class SieveOfEratosthenes
  9. {
  10.     static void Main()
  11.     {
  12.         bool[] allNums = new bool[10000000];
  13.         for (int i = 2; i < Math.Sqrt(allNums.Length) ; i++)
  14.         {
  15.             if (allNums[i] == false )
  16.             {
  17.                 for (int j = i*i; j < allNums.Length; j = j + i)
  18.                 {
  19.                     allNums[j] = true;
  20.                 }
  21.             }
  22.         }
  23.         for (int i = 2; i < allNums.Length; i++)
  24.         {
  25.             if (allNums[i] == false)
  26.             {
  27.                 Console.Write("{0} ",i);
  28.             }
  29.         }
  30.         Console.WriteLine();
  31.     }
  32. }
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