Prime Digit Replacements
By replacing the 1st digit of the 2-digit number \3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime. By replacing the 3rd and 4th digits of 56\\3 with...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
By replacing the \(1^{st}\) digit of the 2-digit number *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.
By replacing the \(3^{rd}\) and \(4^{th}\) digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.
Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.
Problem 51: Prime Digit Replacements
Mathematical Development
Formal Development
Definition 1 (Digit Replacement Family). Let be a positive integer with -digit decimal representation . A digit replacement mask is a nonempty subset . The replacement family is the multiset
where is obtained from by setting for all . The condition that has digits excludes leading zeros (i.e., if , then is excluded).
Definition 2 (Prime Family Size). The prime family size of is
Theorem 1 (Divisibility-by-3 Obstruction). Let . If , then .
Proof. Write the digit sum of as
where is the sum of fixed digits. Since is divisible by 3 if and only if , we require
Case . Since is invertible modulo 3, equation (1) has a unique solution . Among , each residue class modulo 3 contains the following number of elements:
- : , giving 4 elements,
- : , giving 3 elements,
- : , giving 3 elements.
Thus the residue class satisfying (1) contains at least 3 values of . Each corresponding is divisible by 3 and hence composite (since for ). Therefore at most family members can be prime.
Case . Then for every , so equation (1) reduces to , which is independent of . If , then none of the ten candidates are divisible by 3, permitting .
Since an 8-prime family requires , Case 1 is impossible.
Corollary 1. Any mask achieving must satisfy . The minimum such mask size is .
Lemma 1 (Five-Digit Insufficiency). No 5-digit number admits an 8-prime replacement family.
Proof. For 5-digit numbers, feasible mask sizes divisible by 3 are (since ). With replaced positions among 5 digits, there are possible masks per digit group. If (the leading digit is masked), then is excluded, leaving at most 9 candidates. Even with , all 10 candidates are available, but at least 2 must be composite by other divisibility constraints or probabilistic density arguments. Exhaustive computation over all 5-digit primes confirms that no mask achieves .
Corollary 2 (Search Domain). The search begins at 6-digit numbers () with mask size .
Theorem 2 (Smallest Member Constraint). If is the smallest prime in an 8-prime family with , then the masked digits in must have value .
Proof. Among , at most 2 values of yield composite family members (since means exactly 2 composites). The smallest prime in the family has the smallest valid . If , then there are at most 7 values of remaining (namely ), but we also need exactly 8 primes total, requiring 8 values of yielding primes. The smallest such must therefore satisfy so that at least 8 candidates (, minus leading-zero exclusions and composites) are available.
Editorial
We sieve the primes below one million and then scan the six-digit primes in increasing order. For each prime, only the repeated digits 0, 1, and 2 are considered, since the smallest member of an eight-prime family must use one of those masked values. Among the positions carrying such a digit, we enumerate masks whose sizes are divisible by 3, replace all masked positions by a common digit from 0 through 9, discard leading-zero cases, and count how many resulting numbers remain prime. The first family of size eight yields the desired smallest member.
Pseudocode
Algorithm: Smallest Prime in an Eight-prime Digit-replacement Family
Require: The prime table below 10^6.
Ensure: The smallest prime belonging to an eight-prime family obtained by replacing repeated digits in common positions.
1: Build a prime lookup table on {0, 1, ..., 10^6 - 1}.
2: For each six-digit prime n in increasing order, group its digit positions by digit value.
3: For each digit v in {0, 1, 2} present in n and each subset M of its positions with |M| divisible by 3, generate the family produced by replacing all positions in M by a common digit delta in {0, 1, ..., 9}.
4: Discard outcomes with a leading zero; count the prime members of the family and record its smallest member.
5: Return the smallest member of the first family whose prime count is 8.
Complexity Analysis
Proposition 1 (Time Complexity). The algorithm runs in time where .
Proof. The Sieve of Eratosthenes computes is_prime[0..N-1] in time. The main loop iterates over 6-digit primes. By the Prime Number Theorem, . For each prime, we examine digit values and enumerate subsets of size 3 from positions holding digit . The number of such subsets is at most per prime (achieved when all 6 digits are identical). For each subset, we perform 10 replacement checks, each in (digit manipulation and sieve lookup). Thus the mask-checking phase costs at most operations, which is . The total is dominated by the sieve: .
Proposition 2 (Space Complexity). The algorithm uses space for the sieve array.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
const int LIMIT = 1000000;
vector<bool> is_prime(LIMIT, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i < LIMIT; i++)
if (is_prime[i])
for (int j = i * i; j < LIMIT; j += i)
is_prime[j] = false;
for (int n = 100000; n < LIMIT; n++) {
if (!is_prime[n]) continue;
string s = to_string(n);
int len = s.size();
// Try masks where all masked positions share the same digit (0, 1, or 2)
for (int mask = 1; mask < (1 << len); mask++) {
int bits = __builtin_popcount(mask);
if (bits % 3 != 0) continue;
// All masked positions must share the same digit
char common = 0;
bool valid = true;
for (int i = 0; i < len; i++) {
if (mask & (1 << i)) {
if (common == 0) common = s[i];
else if (s[i] != common) { valid = false; break; }
}
}
if (!valid) continue;
// Only check digits 0, 1, 2 for smallest-member constraint
if (common > '2') continue;
int count = 0;
int smallest = -1;
for (char d = '0'; d <= '9'; d++) {
string t = s;
for (int i = 0; i < len; i++)
if (mask & (1 << i))
t[i] = d;
if (t[0] == '0') continue;
int num = stoi(t);
if (num >= 100000 && is_prime[num]) {
count++;
if (smallest == -1) smallest = num;
}
}
if (count == 8) {
cout << smallest << endl;
return 0;
}
}
}
return 0;
}
"""
Problem 51: Prime Digit Replacements
Find the smallest prime which, by replacing part of the number
(not necessarily adjacent digits) with the same digit, is part
of an eight prime value family.
"""
from itertools import combinations
def sieve(limit):
is_prime = [False, False] + [True] * (limit - 2)
for i in range(2, int(limit**0.5) + 1):
if is_prime[i]:
for j in range(i * i, limit, i):
is_prime[j] = False
return is_prime
def solve():
LIMIT = 1_000_000
is_prime = sieve(LIMIT)
for n in range(100000, LIMIT):
if not is_prime[n]:
continue
s = str(n)
# Group positions by digit value
digit_positions = {}
for i, ch in enumerate(s):
digit_positions.setdefault(ch, []).append(i)
# Only check digits 0, 1, 2 (Theorem 2: smallest member constraint)
for digit in ('0', '1', '2'):
if digit not in digit_positions:
continue
positions = digit_positions[digit]
for k in range(3, len(positions) + 1, 3):
for combo in combinations(positions, k):
count = 0
smallest = None
for d in range(10):
t = list(s)
for pos in combo:
t[pos] = str(d)
if t[0] == '0':
continue
num = int(''.join(t))
if num >= 100000 and is_prime[num]:
count += 1
if smallest is None:
smallest = num
if count == 8:
print(smallest)
return
solve()