Example in the overview
R.Tenton
Tentresh at web.de
Sat May 8 16:01:51 PDT 2010
At the very bottom of http://digitalmars.com/d/2.0/overview.html
there is an example implementation of the Eratosthenes' sieve.
That code is broken! It counts 1899 prime numbers, while there are only 1028
primes in the interval [1,8191]!
What is the outermost for loop good for?
This example is just screwed up!
And this was the first D source I ever encountered...
I am referring to the following:
/* Sieve of Eratosthenes prime numbers */
import std.stdio;
bool[8191] flags;
int main()
{ int i, count, prime, k, iter;
writefln("10 iterations");
for (iter = 1; iter <= 10; iter++)
{ count = 0;
flags[] = 1;
for (i = 0; i < flags.length; i++)
{ if (flags[i])
{ prime = i + i + 3;
k = i + prime;
while (k < flags.length)
{
flags[k] = 0;
k += prime;
}
count += 1;
}
}
}
writefln("%d primes", count);
return 0;
}
More information about the Digitalmars-d-learn
mailing list