-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prime-sieve.cpp
31 lines (30 loc) · 1.15 KB
/
prime-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
//Sieve of Eratosthenes
//Print all prime numbers within given range.
#include<bits/stdc++.h>
using namespace std;
void PrimeSieve(int n) {
vector <int> sieve(n + 1); //Create a vector array of size n+1
fill(sieve.begin(), sieve.end(), 1); //Fill the array with 1s, which means all numbers are initially assumed to be prime.
sieve[0] = 0; //Exception:Zero is not a Prime
sieve[1] = 0; //Exception:One is not a Prime
for (int i = 2; i * i <= n; i++) {
if (sieve[i] == 1) { //This condition will only be executed if the sieve[i] is prime, mark all the multiples of Prime Numbers as non-prime.
for (int j = i * i; j <= n; j += i) {
sieve[j] = 0;
}
}
}
for (int i = 0; i < n; i++) {
if (sieve[i] == 1) {
cout << i << " ";
}
}
return;
}
int main() {
int n;
cin >> n;
PrimeSieve(n); //Call the function
return 0; //Time complexity : O(n*log(log(n))), Auxiliary Space: O(1)
//Example : n=10 , Output: 2 3 5 7
}