Determine the smallest pair of twin primes A and B, such that when concatenated, A and B will contain all 10 decimal digits.
Call the final two digits of the smaller prime "y" and "z":
option one - if z is (1,3,5,7}, the smallest possible number is a 9-digit number which does not include z+2
option two - if z is 9, there could be an 8-digit number which does not include y+1 or 1, since adding 2 will convert ......y9 into .....(y+1)1
limit the initial search to 8-digit numbers ending in 9 and which contain neither 1 or the penultimate digit plus one.
The program ran less than one second.
(20487359, 20487361)
--------------------
def isprime(n):
'''check if integer n is a prime'''
n = abs(int(n))
if n < 2:
return False
if n == 2:
return True
if not n & 1:
return False
for x in range(3, int(n**0.5)+1, 2):
if n % x == 0:
return False
return True
for i in range(10000009,100000000,10):
s = str(i)
if '1' in s:
continue
if len(set(s)) != 8:
continue
if str(int(s[6])+1) in s:
continue
if isprime(i) and isprime(i+2):
print('({}, {})'.format(i, i+2))
break
|
Posted by Larry
on 2023-10-20 11:59:44 |