首页 > 其他分享 >素数的埃式筛选法

素数的埃式筛选法

时间:2023-02-17 15:05:02浏览次数:36  
标签:prime cnt 埃式 int 素数 false 筛选 const


const int N = 1e7 ;
int prime[N] ; // 第i 个素数
bool is_prime[N];
int sieve(int n ){
int cnt = 0 ;
for(int i = 0 ; i<=n+1 ;i++){
is_prime[i] = true;
}
is_prime[0] = is_prime[1] = false ;
for(int i = 2 ; i<=n ;i++){
if(is_prime[i]){
prime[cnt++] = i ;
for(int j = 2*i;j<=n;j+=i){
is_prime[j] = false ;
}
}
}
return cnt ; // 返回 n 以内素数个数
}

 

 

标签:prime,cnt,埃式,int,素数,false,筛选,const
From: https://blog.51cto.com/u_15970235/6064104

相关文章