-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimes.cpp
More file actions
50 lines (46 loc) · 990 Bytes
/
Copy pathPrimes.cpp
File metadata and controls
50 lines (46 loc) · 990 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Author: Kevin Li
* Lang: C++
* Description: determines primality of first n integer via Erastothenes Sieve
*/
#include <iostream>
using namespace std;
struct sieve {
int n;
bool *prime;
sieve () {}
sieve (int _n) : n(_n) {
prime = new bool[n+1];
prime[0] = false;
prime[1] = false;
for (int i = 2; i < n+1; i++) {
prime[i] = true;
}
}
void driver(int _n) {
for (int i = 2; i < _n+1; i++) {
if (prime[i]) {
for (int j = 2*i; j < _n+1; j += i) {
prime[j] = false;
}
}
}
}
void print(int _n) {
for (int i = 0; i < _n+1; i++) {
if (prime[i]) {
cout << "1";
} else {
cout << "0";
}
}
cout << endl;
}
};
int a,b,c;
int main() {
cin >> a >> b >> c;
sieve S = sieve(a);
S.driver(b);
S.print(c);
}