-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem0087.cpp
61 lines (48 loc) · 1.2 KB
/
problem0087.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
60
61
#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#define MAX 8000
#define LIMIT 50000000
void del_mult(int p, bool s[]);
int next_prime(int p, bool s[]);
int count(std::vector<int> p);
int main() {
bool sieve[MAX];
for (int i = 0; i < MAX; ++i)
sieve[i] = true;
sieve[0] = false;
sieve[1] = false;
int prime = 2;
for (int i = 0; i * i < MAX; ++i) {
del_mult(prime, sieve);
prime = next_prime(prime, sieve);
}
std::vector<int> primes;
for (int i = 0; i < MAX; ++i) {
if (sieve[i])
primes.push_back(i);
}
std::cout << count(primes) << std::endl;
return 0;
}
int next_prime(int p, bool s[]) {
while (!s[++p]);
return p;
}
void del_mult(int p, bool s[]) {
for (int i = p * p; i < MAX; i += p)
s[i] = false;
}
int count(std::vector<int> p) {
std::set<int> sums;
int sum;
for (int i = 0; (pow(p[i], 2)) < LIMIT; ++i) {
for (int j = 0; (pow(p[i], 2) + pow(p[j], 3)) < LIMIT; ++j) {
for (int k = 0; (sum = (pow(p[i], 2) + pow(p[j], 3) + pow(p[k], 4))) < LIMIT; ++k) {
sums.insert(sum);
}
}
}
return sums.size();
}