-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem027.cpp
More file actions
44 lines (41 loc) · 1.23 KB
/
problem027.cpp
File metadata and controls
44 lines (41 loc) · 1.23 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
#include "lib/mathutils.h"
#include "lib/primesieve.h"
#include <iostream>
#include <utility>
std::pair<int, int> eulerNumbers()
{
int bestA, bestB, bestN = 0;
PrimeSieve sieve(999);
int primes = sieve.countPrimes();
// b must be prime or n^2 + an + b will not produce a prime when n = 0.
// b = 2 is skipped since it stops producing primes when n reaches 2 or
// earlier.
for (int i = 2; i <= primes; ++i) {
int b = sieve.nthPrime(i);
// Skipping the only even value of b allows even values of a to be
// skipped since a must be odd for the equation to work when b is odd
// and n = 1.
for (int a = -999; a <= 999; a += 2) {
int n = 0;
for (;;) {
int c = n * (a + n) + b;
if (c < 2 || !((c <= 999) ? sieve.isPrime(c) : isPrime(c))) {
break;
}
++n;
}
if (n > bestN) {
bestA = a;
bestB = b;
bestN = n;
}
}
}
return std::make_pair(bestA, bestB);
}
int main()
{
std::pair<int, int> ans = eulerNumbers();
std::cout << "Answer: " << ans.first * ans.second << '\n';
return 0;
}