Advertisement
ScorpS

Sieve Of Eratosthenes Algorithm

Jan 7th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.78 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. using System.Collections.Generic;
  8.  
  9. class SieveOfEratosthenesAlgorithm
  10. {
  11.     static void Main()
  12.     {
  13.         List<int> numbers = new List<int>();
  14.         int count = 10000000;
  15.         for (int i = 0; i <= count; i++)
  16.         {
  17.             numbers.Add(i);
  18.         }
  19.  
  20.         for (int i = 2; i < count; i++)
  21.         {
  22.             if (numbers[i] != 0)
  23.             {
  24.                 for (int k = numbers[i] * 2; k <= count; k += numbers[i])
  25.                 {
  26.                     numbers[k] = 0;
  27.                 }
  28.                 Console.WriteLine("{0,4} ", numbers[i]);
  29.             }
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement