-
Notifications
You must be signed in to change notification settings - Fork 160
/
SieveofEratosthenes.cpp
59 lines (43 loc) · 1.08 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
/*
Sieve of Eratosthenes algorithm
*/
/*
solution: start from smallest primer number p, print p and mark 2p, 3p, 4p, ...
print the first number q greater than p without mark if it exists, then repeat the steps above for q.
*/
#include <iostream>
using namespace std;
void FlagMultiples(bool arr[], int a, unsigned int num) {
int i = 2;
int temp = i * a;
while ( temp <= num ) {
arr[ temp-1 ] = true;
++i;
temp = i * a;
}
}
void SieveOfEratosthenesAlg(unsigned int num) {
if (num < 2) {
return;
} else {
bool *visited = new bool[num];
for (int k = 0; k < num; k++) {
visited[k] = false;
}
for (unsigned int i = 1; i < num; ++i) {
if ( visited[i] == 0 ) {
cout<<i+1<<" ";
FlagMultiples(visited, i+1, num);
}
}
delete [] visited;
}
}
int main() {
unsigned int num;
cin>>num;
cout<<"The primer numbers below number "<<num<<" are:"<<endl;
SieveOfEratosthenesAlg(num);
cout<<endl;
return 0;
}