Determine the minimum value of a 100- digit prime number such that we will obtain two other prime numbers by changing its leftmost digit.
Program output (edited for readability) finds these three 100-digit primes:
1000 ... 00076353,
5000 ... 00076353,
7000 ... 00076353
The problem did not ask for this, but by changing a "
1" to a "
2" in the following line of code:
if len(primeTriplets) >
1:
... we get the first 100-digit prime for which changing the first digit obtains
three additional primes:
1000 ... 04817529,
4000 ... 04817529,
5000 ... 04817529,
7000 ... 04817529
------ Python with use of sympy library ------
import sympy as sp
big = int('1' + '0' * 99)
first = sp.nextprime(big)
thisPrime = first
digits = [i for i in range(1,10)]
found = False
solutions = []
while not found:
primeTriplets = []
stp = str(thisPrime)
dfirst = stp[0]
rest = stp[1:]
nfirst = int(dfirst)
testDigits = [i for i in digits if i != nfirst]
for d in testDigits:
test = int(str(d) + rest)
if sp.isprime(test):
primeTriplets.append(test)
if len(primeTriplets) >1:
primeTriplets.insert(0,thisPrime)
solutions.append(primeTriplets)
print(primeTriplets)
found = True
nextP = sp.nextprime(thisPrime)
thisPrime = nextP
|
Posted by Larry
on 2023-01-19 13:11:50 |