What is the largest palindromic prime which remains prime as the two digits at each end are peeled off, one pair at a time, until only a 3-digit prime is left?
For example, from the palindromic prime 30961016903, peel off the end digits 30 and 03 to get the palindromic prime 9610169, then peel off 96 and 69, to leave the prime 101.
[I first misread the problem and added single digits to each end, rather than pairs of digits.
If you peel just one digit at a time, the longest peeled palindromes prime I found are 9 digits of which there are three:
373929373, 733929337, 933101339.
But this was the answer to the wrong question.]
The correct problem ...
For the problem as stated, peeling two digits at a time from each end, I found a 111-digit prime:
979219789296183199171918129575193615723878
123013727418313814727310321878327516391575
921819171991381692987912979
The program first makes a list of 3 digit palprimes, then builds the next list with 4 more digits checking all allowable 2-digit pairs (listed below). For a pair to be a valid prefix, it cannot start with an even number or a 5 because the reversed prefix is appended to the right end, and must be prime.
At first, the number of palprimes of a given length increases with digit length, but then the list gets smaller with only one value at 111 digits and no palprimes with more digits.
Program Output:
['99', '98', '97', '96', '95', '94', '93', '92', '91', '90', '79', '78', '77', '76', '75', '74', '73', '72', '71', '70', '39', '38', '37', '36', '35', '34', '33', '32', '31', '30', '19', '18', '17', '16', '15', '14', '13', '12', '11']
Count #digits largest
93 7 9978799
372 11 99991819999
1116 15 999978383879999
2672 19 9999331513151339999
5317 23 99999911127872111999999
8890 27 999997357878313878753799999
12910 31 9999999735787831387875379999999
16850 35 99999977139339329192393393177999999
19393 39 999996341439721977151779127934143699999
20198 43 9999987375351995169935399615991535737899999
19525 47 99999979751892939999399199399993929815797999999
17295 51 999999717290169911341832181238143119961092717999999
14024 55 9999991772733534129570907592957090759214353372771999999
10856 59 99999992157933353412957090759295709075921435333975129999999
7662 63 999992917733159793117213743990787099347312711397951337719299999
5063 67 9999993872999112339032767218179978799718127672309332119992783999999
3249 71 99999377343938737131957677907612759295721670977675913173783934377399999
1956 75 999993747570389711331390727830747678191876747038727093133117983075747399999
1154 79 9999931196909696943035721938181596367392937636951818391275303496969096911399999
668 83 99999597751535799036323673903293397695901510959679339230937632363099753515779599999
344 87 999996171294121497143796743177767113779777727777977311767771347697341794121492171699999
159 91 9978181378983997127778121936923730303996179779779716993030373296391218777217993898731818799
100 95 99939678113738169372779991773397939038907975777277757970983093979337719997727396183731187693999
52 99 993934323192793273951617319897357672389913777971151179777319983276753798913716159372397291323439399
25 103 9996799396781137381693727799917733979390389079757772777579709830939793377199977273961837311876939976999
7 107 96397236901891397838399136337815741612127572779991153535119997727572121614751873363199383879319810963279369
1 111 979219789296183199171918129575193615723878123013727418313814727310321878327516391575921819171991381692987912979
0
0
0
-------------
import sympy
def isprime(n):
return sympy.isprime(n)
p3pals = [p for p in range(999,100,-2) if isprime(p) and str(p) == str(p)[::-1]]
pairs = [str(a).zfill(2) for a in range(99,10,-1) if (a//10)%2 != 0 and (a//10) != 5]
print(pairs)
listOfPpals = [p3pals]
for reps in range(30):
nextlist = []
for pair in pairs:
for p in listOfPpals[-1]:
candidate = int(pair + str(p) + pair[::-1])
if isprime(candidate):
nextlist.append(candidate)
nextlist = sorted(nextlist)
listOfPpals.append(nextlist)
try:
print( len(nextlist), 7+4*reps, nextlist[-1])
except:
print( len(nextlist))
|
Posted by Larry
on 2024-06-02 09:39:45 |