1e7内比欧拉筛子快一倍,2e7持平,之后略慢 不论N多大整体计算次数,都是欧拉筛子的1/3,求大神解答1e8之后为什么变慢
思路:相较于欧拉筛考虑1-n所有数,这里基于孪生素数,只需要考虑孪生素数,n/3;
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e7+9;
bool a[N+2];
int b[N+2];
int idx=0;
int w=0,s;
clock_t startTime;
int getCurrentTime() {
return 1000.0 * (clock() - startTime) / CLOCKS_PER_SEC;
}
void init(int N){//孪生素数
a[3]=a[2]=true;
for(int i=5;i<N;i+=6) a[i+2]=a[i]=true;
for(int i=5;i<=N/5;i+=6){
b[idx++]=i;
b[idx++]=i+2;
}
for(int i=0;i<idx;i++)
for(int j=0;j<=i&&b[j]*b[i]<N;j++)
a[b[i]*b[j]]=false;
}
bool prime[N];
int p[N], tot;
void init2(int N){//欧拉
for(int i = 2; i < N; i ++) prime[i] = true;//w++;
for(int i = 2; i < N; i++){
if(prime[i]) p[tot ++] = i;
for(int j = 0; j < tot && i * p[j] < N; j++){
prime[i * p[j]] = false;// w++;
if(i % p[j] == 0) break; //w++;
}
}
}
int main()
{
int n;
init(N);
cout<<"my :"<<w<<endl;
cerr << "Execution time: " << getCurrentTime() << " ms." << endl;
init2(N);
cout<<"欧拉 :"<<w<<endl;
cerr << "Execution time: " << getCurrentTime() << " ms." << endl;
return 0;
}