Advertisement
Teodor92

14. SieveOfEro

Jan 6th, 2013
1,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement