筛法求小于n的所有素数

#include <bits/stdc++.h>
using namespace std;

#define N 200000000
bool prime[N];

int main() {
    clock_t start, end;
    double duration;
    start = clock();

    for (int i = 2; i * i < N; i++)
        if (!prime[i])
            for (int j = i * i; j < N; j += i)
                prime[j] = true;
    int cnt = 0;
    for (int i = 2; i < N; i++)
        if (!prime[i]) {
            printf("%d\n", i);
            cnt++;
        }
    printf("共%d个\n", cnt);

    end = clock();
    duration = (double)(end - start) / CLOCKS_PER_SEC;
    printf("%f seconds.\n", duration);
    return 0;
}