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: nob...@puremagic.com ReportedBy: bearophile_h...@eml.cc --- Comment #0 from bearophile_h...@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: -------