-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp111.cpp
More file actions
162 lines (146 loc) · 4.31 KB
/
Copy pathp111.cpp
File metadata and controls
162 lines (146 loc) · 4.31 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* There are nine 4-digit primes containing three ones:
*
* 1117, 1151, 1171, 1181, 1511, 1811, 2111, 4111, 8111
*
* We shall say that M(n, d) represents the maximum number of repeated digits
* for an n-digit prime where d is the repeated digit, N(n, d) represents the
* number of such primes, and S(n, d) represents the sum of these primes.
*
* So M(4, 1) = 3 is the maximum number of repeated digits for a 4-digit prime
* where one is the repeated digit, there are N(4, 1) = 9 such primes, and the
* sum of these primes is S(4, 1) = 22275.
*
* We obtain the following results for 4-digit primes.
*
* -------------------------------
* Digit,d M(4,d) N(4,d) S(4,d)
* -------------------------------
* 0 2 13 67061
* 1 3 9 22275
* 2 3 1 2221
* 3 3 12 46214
* 4 3 2 8888
* 5 3 1 5557
* 6 3 1 6661
* 7 3 9 57863
* 8 3 1 8887
* 9 3 7 48073
* -------------------------------
*
* For d = 0 to 9, the sum of all S(4, d) is 273700.
*
* Find the sum of all S(10, d).
*
* SOLUTION:
*
* Generating all 10-digit primes is too heavy in terms of time and memory.
* Expecting that M(10, d) should be large, we enumerate all 10-digit numbers
* with 10 repeats, 9 repeats, 8 repeats, etc of d, and test for primality.
*/
#include <cstdint>
#include <iostream>
#include <vector>
#include "euler/combination.hpp"
#include "euler/digits.hpp"
#include "euler/prime_test.hpp"
#include "euler.h"
BEGIN_PROBLEM(111, solve_problem_111)
PROBLEM_TITLE("Primes with runs")
PROBLEM_ANSWER("612407567715")
PROBLEM_DIFFICULTY(1)
PROBLEM_FUN_LEVEL(1)
PROBLEM_TIME_COMPLEXITY("")
PROBLEM_SPACE_COMPLEXITY("")
PROBLEM_KEYWORDS("digits,prime")
END_PROBLEM()
// Returns the sum of n-digit primes containing the maximum number of digit d.
static int64_t S(int n, int d)
{
// Build a list of the possible digits in each free place.
// - d cannot appear in any free place;
// - the first digit cannot be zero;
// - the last digit cannot be even.
std::vector<std::vector<int>> allowed(n);
for (int i = 0; i < n; i++)
{
for (int k = 0; k <= 9; k++)
{
if (k == d)
continue;
if (i == 0 && k == 0)
continue;
if (i == n - 1 && k % 2 == 0)
continue;
allowed[i].push_back(k);
}
}
// Allocate a sequence of n digits.
std::vector<int> digits(n);
// Let f be the number of free positions to fill in digits <> d. Start with
// f = 0, i.e. n repeating d. Then try f = 1, i.e. (n - 1) repeating d.
// Continue until we find a prime.
for (int f = 0; f < n; f++)
{
// Let s be the sum of primes with (n - f) repeating d.
int64_t s = 0;
// Iterate each possible choice of f free places out of n places.
for (const std::vector<size_t> &free: euler::choose(n, f))
{
// The most significant position must be free if d == 0.
if (d == 0 && (f == 0 || free[0] != 0))
{
// ?
continue;
}
// Set all digits to d.
std::fill(digits.begin(), digits.end(), d);
// Generate each possible digit tuple in the free places.
std::vector<size_t> free_count(f);
for (int i = 0; i < f; i++)
{
free_count[i] = allowed[free[i]].size();
}
for (const std::vector<size_t> &free_choice:
euler::cartesian(free_count.begin(), free_count.end()))
{
// Construct the number.
for (int i = 0; i < f; ++i)
{
digits[free[i]] = allowed[free[i]][free_choice[i]];
}
// Test whether the number is prime.
int64_t v = euler::from_digits<int64_t>(digits.begin(), digits.end());
if (euler::is_prime(v))
{
if (verbose())
{
std::cout << "Prime: " << v << std::endl;
}
s += v;
}
} // next free
} // next f
// Return if at least one such prime is found.
if (s > 0)
{
return s;
}
}
// Not found.
return 0;
}
static void solve_problem_111()
{
#if 0
const int Digits = 4;
#else
const int Digits = 10;
#endif
int64_t sum = 0;
for (int d = 0; d <= 9; d++)
{
sum += S(Digits, d);
}
std::cout << sum << std::endl;
}