forked from shivprime94/Data-Structure-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsieve_algorithm.cpp
More file actions
33 lines (29 loc) · 970 Bytes
/
sieve_algorithm.cpp
File metadata and controls
33 lines (29 loc) · 970 Bytes
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
// SIEVE OF ERATOSTHENSES (finding prime numbers between [0,n])
// time complexity : O(N * log(log(N)))
//find all prime numbers less than equal to n
#include<bits/stdc++.h>
using namespace std;
void sieve(int n){
vector<bool>isPrime(n+1,true); //true in vector means that the number is prime
isPrime[0] = isPrime[1] = false;
for(int i=2; i<=n; i++){
if(isPrime[i] && i*i <= n) { // check till only sqrt of n
for (int j = i*i; j <= n; j += i){
isPrime[j] = false; // make all the multiple of its to false
}
}
}
// printing all the prime numbers till N
// if the the value is true in bool vector then print i
for(int i=2; i<=n; i++){
if(isPrime[i]){
cout<<i<<" "<<endl;
}
}
}
int main(){
int n;
cin>>n; // checking from 0 --> n
sieve(n);
return 0;
}