-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bitwise Sieve.cpp
52 lines (45 loc) · 1004 Bytes
/
Bitwise Sieve.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
#define MAX 1000000000
#define LL long long int
using namespace std;
int status[(MAX/32)+10];
vector<int>primelist;
bool check(int n, int pos) { return (bool)(n & (1<<pos)); }
int SET(int n, int pos){ return n=n|(1<<pos);}
void sieve()
{
int sqrtN=int (sqrt(MAX));
/*for(int j=4; j<MAX; j=j+2)
status[j>>5]=SET(status[j>>5],j&31);*/
for(int i=3; i<=sqrtN; i=i+2)
{
if(check(status[i>>5],i&31)==0)
{
for(int j=i*i; j<=MAX; j= j + (i<<1))
{
status[j>>5]=SET(status[j>>5],j&31);
}
}
}
primelist.push_back(2);
for(int i=3; i<=MAX; i=i+2)
{
if(check(status[i>>5],i&31)==0){
primelist.push_back(i);
// cout<<i<<endl;
}
}
cout<<primelist.size()<<endl;
}
int main()
{
sieve();
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
printf("%d,",primelist[i]);
}
return 0;
}
//999966000289