Example in the overview

bearophile bearophileHUGS at lycos.com
Sat May 8 17:55:07 PDT 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


More information about the Digitalmars-d-learn mailing list