Primes with Runs
For n -digit primes, define: M(n, d): the maximum number of repeated occurrences of digit d in an n -digit prime, N(n, d): the count of n -digit primes achieving M(n, d) repetitions of d, S(n, d):...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Considering $4$-digit primes containing repeated digits it is clear that they cannot all be the same: $1111$ is divisible by $11$, $2222$ is divisible by $22$, and so on. But 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$. It turns out that for $d = 0$, it is only possible to have $M(4, 0) = 2$ repeated digits, but there are $N(4, 0) = 13$ such cases.
In the same way we obtain the following results for $4$-digit primes.
| Digit, $d$ | $M(4, d)$ | $N(4, d)$ | $S(4, d)$ |
| 0 | 2 | 12 | 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)$.
Problem 111: Primes with Runs
Mathematical Development
Theorem 1 (Repdigit composite bound). A repdigit number with and is composite.
Proof. Write where is the repunit of length . For , we have , so the factorization is nontrivial (both factors exceed 1).
Corollary 1. For all and digits , .
Proof. If , an -digit number cannot have all digits equal to 0 (it would be zero). If , the -digit number is composite by Theorem 1. In both cases, no -digit prime consists entirely of a single repeated digit.
Theorem 2 (Candidate enumeration). For fixed , , and (the number of positions holding digit ), the number of candidate -digit numbers with exactly positions equal to is
where counts candidates with a leading zero.
Proof. Select which of the positions hold a digit other than : there are ways. Each such position admits 9 choices from , yielding candidates in total. However, any candidate with leading digit 0 does not represent a valid -digit number and must be excluded.
Lemma 1 (Deterministic primality for 10-digit numbers). The Miller—Rabin primality test with witness set is deterministic for all integers . In particular, it correctly classifies every 10-digit number.
Proof. Jaeschke (1993) proved that no strong pseudoprime to all bases in exists below . Since every 10-digit number satisfies , the test is infallible in this range.
Theorem 3 (Greedy descent correctness). The algorithm that sets and decreases by 1, at each level exhaustively enumerating all -digit candidates with exactly copies of digit and testing each for primality, correctly determines : the first level at which a prime is found equals .
Proof. By definition, . Corollary 1 gives . The algorithm starts at this upper bound and descends. At each level , it performs an exhaustive search over all valid candidates (Theorem 2) and applies a correct primality test (Lemma 1). Hence, the first yielding at least one prime is the maximum such , which is by definition.
Editorial
The quantity is defined by a maximum repetition count, so the search should start from the top. For a fixed digit , we first ask whether a 10-digit prime exists with nine copies of . If not, we allow two non- positions, then three, and so on. The first repetition count that produces any prime is automatically the maximal one, so all primes found at that level contribute to .
The implementation follows that definition directly. It chooses which positions are allowed to differ from , fills those positions with digits other than , rejects candidates with a leading zero, and runs a deterministic Miller—Rabin test on the resulting 10-digit number. Summing the primes found at the first successful level for each digit gives the required total.
Pseudocode
Set the overall sum to zero.
For each repeated digit d from 0 through 9:
Try repetition counts from 9 downward until a prime appears.
For the current repetition count, choose the positions that are allowed to differ from d.
Fill those free positions with digits other than d to form every candidate number with exactly that many copies of d.
Discard any candidate with a leading zero, and test every remaining candidate for primality.
If at least one prime is found at this level, add their sum to the overall total and stop descending for this digit.
Return the overall sum.
Complexity Analysis
- Time. For each digit , the greedy descent starts at (i.e., free position). At level , the candidate count is . At level , it is . In practice, most digits find primes at or . Over all 10 digits, the total number of candidates tested is at most . Each Miller—Rabin test costs with 6 witnesses, where . Since , each test performs a bounded number of modular exponentiations. Total: (constant with respect to the problem parameters).
- Space. beyond a small list of primes per pair.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 lll;
bool miller_rabin(ll n, ll a) {
if (n % a == 0) return n == a;
ll d = n - 1;
int r = 0;
while (d % 2 == 0) { d /= 2; r++; }
ll x = 1, base = a % n, dd = d;
while (dd > 0) {
if (dd & 1) x = (lll)x * base % n;
base = (lll)base * base % n;
dd >>= 1;
}
if (x == 1 || x == n - 1) return true;
for (int i = 0; i < r - 1; i++) {
x = (lll)x * x % n;
if (x == n - 1) return true;
}
return false;
}
bool is_prime(ll n) {
if (n < 2) return false;
for (ll a : {2, 3, 5, 7, 11, 13})
if (!miller_rabin(n, a)) return false;
return true;
}
int main() {
const int n = 10;
ll total = 0;
for (int d = 0; d <= 9; d++) {
for (int k = n - 1; k >= 1; k--) {
int free = n - k;
vector<int> combo(free);
ll sum_p = 0;
function<void(int, int)> gen_combos = [&](int start, int idx) {
if (idx == free) {
function<void(int, ll)> gen_digits = [&](int i, ll num) {
if (i == n) {
if (num >= 1000000000LL && is_prime(num))
sum_p += num;
return;
}
bool is_free = false;
for (int j = 0; j < free; j++)
if (combo[j] == i) { is_free = true; break; }
if (!is_free) {
gen_digits(i + 1, num * 10 + d);
} else {
for (int dd = 0; dd <= 9; dd++) {
if (dd == d) continue;
gen_digits(i + 1, num * 10 + dd);
}
}
};
gen_digits(0, 0);
return;
}
for (int i = start; i < n; i++) {
combo[idx] = i;
gen_combos(i + 1, idx + 1);
}
};
gen_combos(0, 0);
if (sum_p > 0) { total += sum_p; break; }
}
}
cout << total << endl;
return 0;
}
from itertools import combinations
def is_prime(n):
"""Deterministic Miller-Rabin for n < 3.2 * 10^13 (Jaeschke 1993)."""
if n < 2:
return False
if n < 4:
return True
if n % 2 == 0 or n % 3 == 0:
return False
d, r = n - 1, 0
while d % 2 == 0:
d //= 2
r += 1
for a in (2, 3, 5, 7, 11, 13):
if a >= n:
continue
x = pow(a, d, n)
if x == 1 or x == n - 1:
continue
for _ in range(r - 1):
x = pow(x, 2, n)
if x == n - 1:
break
else:
return False
return True
def solve():
n = 10
total = 0
for d in range(10):
for k in range(n - 1, 0, -1):
free = n - k
sum_primes = 0
for positions in combinations(range(n), free):
pos_set = set(positions)
def generate(idx, num):
nonlocal sum_primes
if idx == n:
if num >= 10 ** (n - 1) and is_prime(num):
sum_primes += num
return
if idx in pos_set:
for dig in range(10):
if dig != d:
generate(idx + 1, num * 10 + dig)
else:
generate(idx + 1, num * 10 + d)
generate(0, 0)
if sum_primes > 0:
total += sum_primes
break
print(total)
solve()