-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem027.cc
More file actions
66 lines (58 loc) · 1.46 KB
/
Copy pathproblem027.cc
File metadata and controls
66 lines (58 loc) · 1.46 KB
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
62
63
64
65
#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>
#include <cmath>
#include <set>
std::vector<unsigned> gen_primes(const unsigned &max);
int main(int argc, char **argv) {
std::vector<unsigned> vprimes = gen_primes(1000u);
std::set<unsigned> primes(vprimes.begin(), vprimes.end());
auto b = std::find(vprimes.begin(), vprimes.end(), 41u);
int saved_a = 1;
int saved_b = 41;
int max = 40;
for (++b; b != vprimes.end(); ++b) {
int sq = std::sqrt(1 - 4 * (41 - *b));
int n[2] = {(-1 + sq) / 2, (-1 - sq) / 2};
int as[2] = {-(2*n[0] + 1), -(2*n[1] + 1)};
for (int a : as) {
int count = 0;
int x = 0;
unsigned px = 0;
do {
++count;// start with this, since p(0) is prime
++x;
px = x * x + a * x + *b;
} while (primes.find(px) != primes.end());
if (count > max) {
saved_a = a;
saved_b = *b;
max = count;
}
}
}
std::cout << saved_a * saved_b << '\n';
return 0;
}
/**
* Uses the primes generator algorithm from TAOCP vol. 1
*/
std::vector<unsigned> gen_primes(const unsigned &max) {
unsigned n = 3u;
std::vector<unsigned> primes;
primes.push_back(2u);
primes.push_back(n);
while (n <= max) {
n += 2;
for (unsigned k = 1; k < primes.size(); ++k) {
if (n % primes[k] == 0)
break;
if (n / primes[k] <= primes[k]) {
primes.push_back(n);
break;
}
}
}
return primes;
}