-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem035.cpp
62 lines (58 loc) · 1.51 KB
/
problem035.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
62
#include "lib/mathutils.h"
#include <iostream>
#include <vector>
bool isCircularPrime(int n)
{
if (!isPrime(n)) {
return false;
}
int digits = 1;
int mult = 1;
int x = n / 10;
while (x != 0) {
++digits;
mult *= 10;
x /= 10;
}
x = n;
for (int i = 1; i < digits; ++i) {
x = mult * (x % 10) + x / 10;
if (!isPrime(x)) {
return false;
}
}
return true;
}
std::vector<int> circularPrimes(int maxDigits)
{
// If a number that is not 2 or 5 contains as much as a single even digit
// or 5, it cannot be a circular prime since one of its rotations will be
// composite. The following code skips numbers with bad digits by iterating
// over the Cartesian product of the good digits with themselves.
int d[] = { 1, 3, 7, 9 };
std::vector<int> results;
results.push_back(2);
results.push_back(5);
int product = 4;
for (int digits = 1; digits <= maxDigits; ++digits) {
for (int i = 0; i < product; ++i) {
int j = i;
int val = 0;
for (int k = 0; k < digits; ++k) {
val = 10 * val + d[j % 4];
j /= 4;
}
if (isCircularPrime(val)) {
results.push_back(val);
}
}
product *= 4;
}
return results;
}
int main()
{
// The exclusive upper limit is one million so 6 digits will do.
std::cout << "Answer: " << circularPrimes(6).size() << '\n';
return 0;
}