-
Notifications
You must be signed in to change notification settings - Fork 0
/
#101.cpp
48 lines (40 loc) · 885 Bytes
/
#101.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
#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<bool> generate_sieve(int n) {
vector<bool> isPrime;
for (int i = 0; i <= n; i++) {
isPrime.push_back(true);
}
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
int count = 2 * i;
while (count <= n) {
isPrime[count] = false;
count += i;
}
}
}
return isPrime;
}
pair<int, int> prime_pair(int n, vector<bool> &sieve) {
for (int i = 2; i < n-1; i++) {
if (sieve[i] && sieve[n - i]) {
return make_pair(i, n - i);
}
}
return make_pair(-1, -1);
}
int main() {
vector<bool> sieve = generate_sieve(10000000);
string inp;
while (cin >> inp) {
int n = stoi(inp);
pair<int, int> ret = prime_pair(n, sieve);
if (ret.first == -1)
cout << "No possible pair of primes." << endl;
else
cout << "Prime pairs: " << ret.first << ", " << ret.second << endl;
}
}