高效素数表模板

高效素数表模板

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<sstream>
 6 using namespace std;
 7 #define N 100000
 8 int h=0;
 9 bool p[N];
10 int prime[N];
11  
12 void db()
13 {
14     memset(p,true,sizeof(p));
15     for(int i=2;i<N;i++)
16     {
17         if(p[i]==true)
18         {
19             prime[h++]=i;
20         }
21         for(int j=0;j<h&&i*prime[j]<N;j++)
22         {
23             p[i*prime[j]]=false;
24             if(i%prime[j]==0)  break;
25         }
26     }
27 }
28 int main()
29 {
30     db();
31     int n;  //打印前n个质数
32     cin>>n;
33     for(int i=0;i<n;i++)
34         cout<<prime[i]<<" ";
35     return 0;
36 }

参考博客:https://blog.csdn.net/sinat_35121480/article/details/53580407

原文地址:https://www.cnblogs.com/weixq351/p/9481242.html