forked from uditkumar489/HacktoberFest-HelloWorld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSieve_of_Eratosthenes.cpp
47 lines (34 loc) · 918 Bytes
/
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
#include <iostream>
using namespace std;
//PRIME SIEVE & OPTIMISATIONS
void primeSieve(int *p,int n){
p[0] = p[1] = 0;
p[2] = 1;
//Let us Mark all Odd Numbers as Prime(Initialisations)
for(int i=3;i<=n;i+=2){
p[i] = 1;
}
//Sieve Login to mark non prime numbers as 0
//1. Optimsation : Iterate only over odd Numbers
for(int i=3;i<=n;i+=2){
if(p[i]){
//Mark all the multiples of that number as not prime.
//2. Optimisation Take a jump of 2i starting from i*i
for(int j=i*i;j<=n;j+=2*i){
p[j] = 0;
}
}
}
return;
}
int main() {
int N = 1000000;
int p[N] = {0};
// you can customise 100 to get the all prime numbers till that number of you choice.
primeSieve(p,100);
for(int i=0;i<=100;i++){
if(p[i]){
cout<<i<<" ";
}
}
}