Jump to page: 1 2
Thread overview
Example in the overview
May 08, 2010
R.Tenton
May 09, 2010
bearophile
May 09, 2010
R.Tenton
May 09, 2010
bearophile
May 09, 2010
R.Tenton
May 09, 2010
bearophile
May 09, 2010
R.Tenton
May 14, 2010
Walter Bright
May 14, 2010
bearophile
May 15, 2010
Walter Bright
May 15, 2010
Ary Borenszweig
May 15, 2010
bearophile
May 15, 2010
bearophile
May 15, 2010
Simen kjaeraas
May 15, 2010
Walter Bright
May 08, 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;
}
May 09, 2010
D2 code, the 8191 too is counted:

import std.stdio: writeln;

int sieve(int m) {
    auto isPrime = new bool[m + 1];
    isPrime[] = true;
    int count;

    foreach (i; 2 .. isPrime.length) {
        if (isPrime[i]) {
            count++;
            for (int k = i * 2; k < isPrime.length; k += i)
                isPrime[k] = false;
        }
    }

    return count;
}

void main() {
    writeln(sieve(8191));
}


Bye,
bearophile
May 09, 2010
The sieve algorith was not my problem. I wanted to point out that
the example is totally screwed up, so it would be corrected.
Or is this the wrong place to ask?
May 09, 2010
R.Tenton:
> Or is this the wrong place to ask?

The right place to report errors in the docs is the bugzilla...
But if you don't want to subscribe some people here can do it for you.

Bye,
bearophile
May 09, 2010
That would be nice.
May 09, 2010
R.Tenton:
> That would be nice.

http://d.puremagic.com/issues/show_bug.cgi?id=4164
May 09, 2010
Thanks!
May 14, 2010
R.Tenton wrote:
> 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]!

Are you sure? What's the mistake in the code?

> What is the outermost for loop good for?

It is a translation of the C benchmark that was common in the 1980's. The outermost loop is to make it take longer so the test can be timed better.

The original:

http://home.iae.nl/users/mhx/nsieve.c

The more common later version:

http://www.paxcompiler.com/js_sieve.htm
May 14, 2010
Walter Bright:
> Are you sure? What's the mistake in the code?

This is the code in the Overview, it prints 1899: http://codepad.org/lzRtggEL

This is the code I have suggested in bugzilla, it prints 1027: http://ideone.com/D9ZqQ

Wolfram Alpha says they are 1027 (I have left out the last number because Alpha uses a closed interval) http://www.wolframalpha.com/input/?i=primes+in+2+..+8190

Bye,
bearophile
May 15, 2010
bearophile wrote:
> Walter Bright:
>> Are you sure? What's the mistake in the code?
> 
> This is the code in the Overview, it prints 1899:
> http://codepad.org/lzRtggEL
> 
> This is the code I have suggested in bugzilla, it prints 1027:
> http://ideone.com/D9ZqQ
> 
> Wolfram Alpha says they are 1027 (I have left out the last number because Alpha uses a closed interval)
> http://www.wolframalpha.com/input/?i=primes+in+2+..+8190


It's been printing 1899 primes since the 80's. Just for fun, google [sieve of eratosthenes 1899 primes]
« First   ‹ Prev
1 2