标签:欧拉 prim mem bre 线性筛 space end its oid
#include<bits/stdc++.h> using namespace std; #define maxn 40 int prime[maxn]; int visit[maxn]; void Prime(){//埃氏筛法 memset(visit,0,sizeof(visit)); //初始化都是素数 visit[0] = visit[1] = 1; //0 和 1不是素数 for (int i = 2; i <= maxn; i++) { if (!visit[i]) { //如果i是素数,让i的所有倍数都不是素数 for (int j = i*i; j <= maxn; j += i) { visit[j] = 1; } } } } /*void Prime(){//欧拉筛法:在上面基础上不会重复 如2*8=16 4*4=16 int cnt=0; memset(visit,0,sizeof(visit)); memset(prime,0,sizeof(prime)); for (int i = 2;i <= maxn; i++) { if(!visit[i]) { prime[++cnt]=i; } for (int j=1;j<=cnt&&i*prime[j]<=maxn;j++) { //cout<<" i = "<<i<<" j = "<<j<<" prime["<<j<<"]"<<" = "<<prime[j]<<" i*prime[j] = "<<i*prime[j]<<endl; visit[i*prime[j]]=1; if(i%prime[j]==0) break; } } } */ int main() { Prime(); for(int i=2;i<=maxn;i++) { if(visit[i]==0) cout<<i<<endl; } return 0; }
素数筛选
标签:欧拉 prim mem bre 线性筛 space end its oid
原文地址:https://www.cnblogs.com/lhlccc/p/12001386.html