forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsieve_of_eratosthenes.cpp
49 lines (42 loc) · 1.01 KB
/
sieve_of_eratosthenes.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
// this is a program to implement sieve algorithm to generate prime numbers.
//https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int,ll int>
#define vi vector<ll int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define hell 1000000007
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
int prime[1000000]={0};
void sieve(int n)
{
for(int i=2;i<=n;i++)
{
if(prime[i]==0)
{
for(int j=2*i;j<=n;j+=i)
prime[j]=1;
}
}
}
int main()
{
ios
int x;
cin>>x;
sieve(x);
//now whichever i>1 has prime[i]==0 , is a prime.
for(int i=2;i<=x;i++)
{
if(prime[i]==0)
cout<<i<<" ";
}
}