Back to Project Euler

Previous Problem Next Problem

Problem 51: 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 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.

Solution

From this point onward, the problems start to get a bit harder.

As mentioned in my writeup of Problem 41, a number is divisible by 3 if and only if the sum of its digits is divisible by 3. Thus, if the fixed digits of a number family sum to \(x\) modulo 3, the variable digits must never sum to \(-x\) modulo 3. That is, if there are \(k\) variable digits which take on values \(t\), then \(kt\) can take on at most two values modulo \(3\). There are four digits that are equivalent to 0 mod 3; three digits that are equivalent to 1 mod 3; and three digits that are equivalent to 2 mod 3 (in order: 0, 3, 6, and 9; 1, 4, and 7; 2, 5, and 8). Any set of eight \(t\) will have at least one element in all three sets. Therefore, in order to restrict the values of \(kt\) modulo 3, \(k\) must be divisible by 3.

The assumption that \(k = 3\) is reasonable and ultimately correct, as is the assuption that there are three fixed digits. We are thus looking for a family of six-digit numbers. Note that the last digit must be fixed, because odd primes only end in the digits 1, 3, 7, and 9. There are therefore ten possible patterns for a family: AB***C, A*B**C, A**B*C, A***BC, *AB**C, *A*B*C, *A**BC, **AB*C, **A*BC, and ***ABC, where "*" represents a variable digit and "A," "B," and "C" represent fixed digits.

Python Code

from sympy import isprime

def p51():
	patterns = ['{0}{1}{3}{3}{3}{2}', '{0}{3}{1}{3}{3}{2}',
				'{0}{3}{3}{1}{3}{2}', '{0}{3}{3}{3}{1}{2}',
				'{3}{0}{1}{3}{3}{2}', '{3}{0}{3}{1}{3}{2}',
				'{3}{0}{3}{3}{1}{2}', '{3}{3}{0}{1}{3}{2}',
				'{3}{3}{0}{3}{1}{2}', '{3}{3}{3}{0}{1}{2}']
	res = 10 ** 6  # Just in case there were multiple families (there aren't)
	for pattern in patterns:
		for A in range(10):
			if pattern[1] == '0' and A == 0:
				continue
			for B in range(10):
				for C in [1, 3, 7, 9]:
					tmin = 1 if pattern[1] == '3' else 0
					non_primes = tmin
					pmin = int(pattern.format(A, B, C, tmin))
					for t in range(tmin, 10):
						p = int(pattern.format(A, B, C, t))
						if not isprime(p):
							non_primes += 1
							if non_primes > 2:
								break
					if non_primes == 2:
						res = min(res, pmin)
	return res

Previous Problem Next Problem

Back to Project Euler