-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem041.cpp
More file actions
31 lines (28 loc) · 790 Bytes
/
problem041.cpp
File metadata and controls
31 lines (28 loc) · 790 Bytes
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
#include "lib/mathutils.h"
#include <algorithm>
#include <iostream>
int largestPandigitalPrime()
{
int a[] = { 7, 6, 5, 4, 3, 2, 1 };
int offsets[] = { 0, 3 };
// Checks 7-digit pandigitals and then 4-digit ones if necessary. All other
// pandigitals aside from 1 are divisible by 3 since their digit sums are
// divisible by 3.
for (int i = 0; i < 2; ++i) {
int start = offsets[i];
do {
int val = 0;
for (int j = start; j < 7; ++j) {
val = 10 * val + a[j];
}
if (isPrime(val)) {
return val;
}
} while (std::prev_permutation(a + start, a + 7));
}
}
int main()
{
std::cout << "Answer: " << largestPandigitalPrime() << '\n';
return 0;
}