-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP686.cpp
57 lines (52 loc) · 1.29 KB
/
P686.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
#include <iostream>
#include <vector>
bool isPrime(unsigned int p, const std::vector<unsigned int> &primes) {
for(std::vector<unsigned int>::const_iterator it = primes.begin(); it != primes.end(); ++it) {
if(p % *it == 0)
return false;
}
return true;
}
unsigned int upperBound(unsigned int n, const std::vector<unsigned int> &p) {
unsigned int low = 0;
unsigned int high = p.size()-1;
while(low < high-1) {
unsigned int mid = (low+high)/2;
if(n < p[mid])
high = mid;
else
low = mid;
}
return high;
}
int main() {
// Compute prime numbers:
std::vector<unsigned int> primes;
primes.push_back(3);
primes.push_back(5);
for(unsigned int i = 7; i < 32768; i+=2) {
if(isPrime(i, primes))
primes.push_back(i);
}
unsigned int n;
while(true) {
std::cin >> n;
if(n == 0)
return 0;
if(n == 4) {
std::cout << 1 << std::endl;
continue;
}
unsigned int top = upperBound(n,primes);
int cnt = 0;
for(unsigned int bottom = 0; top > bottom; ++bottom) {
while(primes[top]+primes[bottom] > n && top > bottom)
--top;
if(primes[top]+primes[bottom] == n) {
//std::cerr << n << " = " << primes[top] << "+" << primes[bottom] << std::endl;
++cnt;
}
}
std::cout << cnt << std::endl;
}
}