// Program: eratosthenes.cpp // Calculate prime numbers in {2,...,n-1} using // Eratosthenes' sieve. #include int main() { const int n = 1000; // definition and initialization: provides us with // Booleans crossed_out[0],..., crossed_out[n-1] bool crossed_out[n]; for (int i = 0; i < n; ++i) crossed_out[i] = false; // computation and output std::cout << "Prime numbers in {2,...," << n-1 << "}:\n"; for (int i = 2; i < n; ++i) if (!crossed_out[i]) { // i is prime std::cout << i << " "; // cross out all proper multiples of i for (int m = 2*i; m < n; m += i) crossed_out[m] = true; } std::cout << "\n"; return 0; }