Erastothenes

davidb ta-nospam-zz at gmx.at
Thu Jun 14 15:35:11 PDT 2007


Stewart Gordon wrote:
> 
> "davidb" <ta-nospam-zz at gmx.at> wrote in message 
> news:f4rvk2$1u7k$1 at digitalmars.com...
>> Dave Colling wrote:
>>> Has anyone confirmed that the example showing how to find primes
>>> by the sieve of Erastothenes actuall works?
>>> It certainly counts something, but they are not primes!
>>
>> You're right, definitely not (includes 0, 1, 4, 8, 10, 14, 20, 22, 25, 
>> ...)
>> It still features printf instead of writef and is in my opinion
>> rather confusing for someone starting to learn by example.
>> So here's a cleaned up version, placed in the public domain (hint)
> 
> It does work.  You appear to have not seen the line
> 
>    prime = i + i + 3;
> 
> I added this line
> 
>    writefln("i = %d; prime = %d", i, prime);
> 
> at the end of the if block to see what's happening.
> 
> But there are a few flaws with it.  It sets a bad example not only by 
> using printf, but also by being badly commented and by declaring 
> everything in one place.  And by using 1 and 0 instead of true and false.
> 
> But 3 to 16383 seems a peculiar range to count.  If only it added 1 to 
> the count, it would give the number of primes below 1 << 14, which seems 
> more what one would expect.
> 
> And if multiple iterations are meant to be for benchmarking, 10 is 
> nowhere near enough on modern systems.
> 
> Stewart.

Yes, I wrote too fast. But what I did was to check flags[],
which indicates all these numbers as primes...
(which would be the intuitive result at least to me
and is the usual approach as far as I have sieve's encountered so far).


David


More information about the Digitalmars-d-learn mailing list