Advertisement
simonses

EratosthenesAlgorithmPrimeNumbers

Jan 13th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class EratosthenesAlgorithmPrimeNumbers
  5. {
  6.     static void Main()
  7.     {
  8.         DateTime a1 = DateTime.Now;
  9.         // Define the Array
  10.         int length = 10000000;
  11.         bool[] array = new bool[length];
  12.         List<long> arrayPrime = new List<long>();
  13.  
  14.         for (int i = 0; i < array.Length; i++)
  15.         {
  16.             array[i] = true;
  17.         }
  18.  
  19.         // Get the prime numbers "Eratosthenes Algorithm"
  20.         long j = 0;
  21.         for (long i = 2; i < array.Length; i++)
  22.         {
  23.             if (array[i] == true)
  24.             {
  25.                 arrayPrime.Add(i);
  26.                 j = i * i;
  27.             }
  28.             while (j < array.Length)
  29.             {
  30.                 array[j] = false;
  31.                 j = j + i;
  32.             }
  33.         }
  34.  
  35.         // Print
  36.         for (int i = 0; i < arrayPrime.Count - 100; i++)
  37.         {
  38.             Console.Write(arrayPrime[i] + " ");
  39.         }
  40.         Console.WriteLine();
  41.         DateTime a2 = DateTime.Now;
  42.         Console.WriteLine(a2 - a1);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement