素数打表

 1 #include<iostream>

 2 #include<stdio.h>

 3 #include<cstring>

 4 #include<cstdlib>

 5 #include<bitset>

 6 using namespace std;

 7 const int maxn = 1e6;

 8 bitset<maxn> prime;

 9 

10 void init()

11 {

12     int i,j;

13     prime.set();

14     for(i=4;i<=maxn;i=i+2)prime[i]=false;

15     for(i=3;(j=i*i)<maxn;i=i+2)

16     {

17         if(prime[i]==false) continue;

18         for(;j<maxn;j=j+i+i)

19             prime[j]=false;

20     }

21 }

22 int main()

23 {

24     init();

25     for(int i=2;i<=100;i++)if(prime[i]==true) printf("%d ",i);

26     return 0;

27 }

你可能感兴趣的:(素数)