Just Code‎ > ‎

Sieve of Eratosthenes

posted Aug 27, 2009, 1:11 AM by Peter Henell
public class ListErot
{
public static void run(int max)
{
int[] lista = new int[(int)Math.Ceiling(max / 2d)];
int pos = 0;
lista[
0] = 2;
for (int i = 3; i < max; i = i + 2)
{
lista[
++pos] = i;

}

for (int i = 3; i < Math.Sqrt(max); i++)
{
lista
= Array.FindAll<int>(lista,
x
=>
x
== i || x % i != 0);
}

long sum = 0;
foreach (var item in lista)
{
sum
+= item;
Console.WriteLine(item
+ "\t");
}
Console.WriteLine(sum);
}
}
Comments