[Issue 4164] New: sieve Sample D Program
    d-bugmail at puremagic.com 
    d-bugmail at puremagic.com
       
    Sun May  9 04:43:08 PDT 2010
    
    
  
http://d.puremagic.com/issues/show_bug.cgi?id=4164
           Summary: sieve Sample D Program
           Product: D
           Version: future
          Platform: All
               URL: http://www.digitalmars.com/webnews/newsgroups.php?art_
                    group=digitalmars.D.learn&article_id=19692
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: www.digitalmars.com
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2010-05-09 04:43:07 PDT ---
R. Tenton in D.learn reports that the Sample D Program (sieve.d) at the bottom
of this page gives a wrong result:
http://digitalmars.com/d/2.0/overview.html
I suggest to replace it with this code that gives a correct result and shows D2
foreach (tested with DMD 2.043):
import std.stdio: writefln;
int sieve(int pmax) {
    if (pmax < 2)
        return 0;
    auto isPrime = new bool[pmax]; // fist initialization
    isPrime[] = true; // second initialization
    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() {
    enum int m = 8191;
    writefln("Primes in [2 .. %d) = %d", m, sieve(m));
}
In 2 .. 8191 there are 1027 primes, not counting 8191.
-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
    
    
More information about the Digitalmars-d-bugs
mailing list