-
Notifications
You must be signed in to change notification settings - Fork 1
/
SieveOfEratosthenes.cpp
64 lines (55 loc) · 1.04 KB
/
SieveOfEratosthenes.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
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
void SieveOfEratosthenes(ull n)
{
bool prime[n+1];
memset(prime, true, sizeof(prime));
for (ull p=2; p*p<=n; p++)
{
if (prime[p])
{
for (ull i=p*2; i<=n; i += p)
prime[i] = false;
}
}
for (ull i=2; i<=n; i++)
if (prime[i])
cout << i <<" ";
}
int main()
{
ull n;
ios_base::sync_with_stdio(false);
// cin.tie(NULL);
while(cin>>n)
{
SieveOfEratosthenes(n);
cout<<'\n';
}
return 0;
}
// ull dp[1000000];
// ull isprime(ull N)
// {
// if(N<2 || (!(N&1) && N!=2))
// return 0;
// for(int i=3; i*i<=N; i+=2){
// if(!(N%i))
// return 0;
// }
// return 1;
// }
// int main()
// {
// ull i, num;
// for(i=0; i<1000000; i++)
// {
// dp[i] = isprime(i);
// }
// for(i=0; i<1000000; i++)
// {
// cout<<i<<' '<<dp[i]<<'\n';
// }
// return 0;
// }