线性筛
线性筛
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e6;
int p[N], cnt;
bool tou[N];
void prime(int n){
for (int i = 2; i <= n; i ++){
if (!tou[i]) p[cnt++] = i; //如果i是质数也就是说没被touch过
for (int j = 0; p[j] <= n / i; j ++){
tou[p[j] * i] = true;
if (i % p[j] == 0) break;
}
}
}
int main(){
int n; cin >> n;
prime(n);
cout << cnt;
}
总结
附录
最后更新于