例题
用处
可以用于判断素数,在面对大数据是比埃式算法快。如遇到
1
≤
N
≤
1
0
8
1 \le N \le 10^8
1≤N≤108的数据是,欧拉筛比埃式筛要快上不少
时间复杂度为O(n)
**#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
int n,m,j,k,s,x;
int b[5800000];
bool a[100000000];
int main(){
cin>>n;
memset(a,true,sizeof(a));
a[1]=true;
for (int i=2;i<=n;i++){
if (a[i]==true){
b[++s]=i;
}
for (int j=1;j<=s;j++){
if (i*b[j]>n){
break;
}
a[i*b[j]]=false;
if (i%b[j]==0){
break;
}
}
}
cout<<s;
return 0;
}**
(n_n);