It is straightforward to test primes like those shown in Penguino's answer, but using fill digits in {1, 3, 7, 9} instead of just {1}. For example, given a prefix, a suffix, a fill digit, and two limits, the testpair
procedure in the following code (chain-primes.py
) makes a list of compatible primes and calculates the score for the listed primes. Here's an example of its use (within ipython
environment):
In [94]: execfile('chain-primes.py')
In [95]: testpair(1234, 901, 9)
Scores 7282 + 819 = 8101
6 123499...............................................
12 123499999999.........................................
20 12349999999999999999.................................
29 12349999999999999999999999999........................
34 1234999999999999999999999999999999...................
48 123499999999999999999999999999999999999999999999.....
49 1234999999999999999999999999999999999999999999999....
27 ..........................999999999999999999999999901
7 ..............................................9999901
5 ................................................99901
4 .................................................9901
For this example, testpair
used findPre
to find primes (like 123499, 123499999999, etc) that begin with 1234
and end with 9*
, ie an arbitrary number of 9's. Then it used findSuf
to find primes (like 9901, 99901, etc) that begin with 9*
and end with 901
. The line Scores 7282 + 819 = 8101
indicates that the seven primes beginning with 1234
score 7282 points, and the four primes ending with 901
score 819 points. Finally, testpair
used shoPre
and shoSuf
to display results.
I wrote additional code (not shown) that (1) calls findPre
to look for high-scoring prefixes for all numbers up to some limit; (2) calls findSuf
to look for high-scoring suffixes that are not multiples of two or five; (3) sorts both data sets into higher-scores-first order; (4) builds lists of chain-compatible prefixes and suffixes; and (5) computes valid scores for likely candidate pairs and displays high scores.
For prefixes and suffixes that contain at most 7 digits, the best result I've found scores 25826 points, as shown next with output from the call testpair(1349812, 198271, 9, 48)
. [The value of 48 for parameter plim
limits prefixed primes to at most 47 digits; by contrast, the less-constrained call testpair(1349812, 198271, 9)
lists a 51-digit prime whose trailing 9's overlap non-9's in the suffix area. The correct total for such a pair is less than the sum of their separate unconstrained scores.]
In [83]: testpair(1349812, 198271, 9, 48)
Scores 13338 + 12488 = 25826
9 134981299............................................
14 13498129999999.......................................
23 13498129999999999999999..............................
32 13498129999999999999999999999999.....................
34 1349812999999999999999999999999999...................
35 13498129999999999999999999999999999..................
39 134981299999999999999999999999999999999..............
40 1349812999999999999999999999999999999999.............
41 13498129999999999999999999999999999999999............
46 1349812999999999999999999999999999999999999999.......
47 13498129999999999999999999999999999999999999999......
46 .......9999999999999999999999999999999999999999198271
45 ........999999999999999999999999999999999999999198271
44 .........99999999999999999999999999999999999999198271
37 ................9999999999999999999999999999999198271
34 ...................9999999999999999999999999999198271
31 ......................9999999999999999999999999198271
30 .......................999999999999999999999999198271
28 .........................9999999999999999999999198271
25 ............................9999999999999999999198271
16 .....................................9999999999198271
14 .......................................99999999198271
10 ...........................................9999198271
8 .............................................99198271
Here is the chain-primes.py
code:
from gmpy import next_prime, numdigits, is_prime
def digitcount(b):
nd = numdigits(b) # May be off-by-one
while 10**nd <= b: nd += 1
while 10**(nd-1) > b: nd -= 1
return nd
def findPre(pre, lim, d):
b, s, p, preDig = pre, 0, [], digitcount(pre)
fillLim = lim-preDig
for i in range(fillLim):
if is_prime(b):
s += (preDig+i)**2
p.append(i)
b = 10*b + d
return s, pre, p
def findSuf(suf, lim, d):
b, s, p, preDig = suf, 0, [], digitcount(suf)
fillLim = lim-preDig
k = d * (10**preDig) # Eg if b is 37 and d is 7, now k=700.
for i in range(fillLim):
if is_prime(b):
s += (preDig+i)**2
p.append(i)
b += k; k *= 10;
return s, suf, p
def shoPre(preSet, lim, d):
sco, pre, llist = preSet
pdig = digitcount(pre)
for l in llist:
print '{:2} {:.<53}'.format(pdig+l, repr(pre) + repr(d)*l)
def shoSuf(sufSet, lim, d):
sco, suf, llist = sufSet
sdig = digitcount(suf)
for l in reversed(llist):
print '{:2} {:.>53}'.format(sdig+l, repr(d)*l + repr(suf))
def testpair(pre, suf, filld, plim=53, slim=53):
preSet = findPre(pre, plim, filld)
sufSet = findSuf(suf, slim, filld)
print 'Scores {} + {} = {}'.format(
preSet[0], sufSet[0], preSet[0]+sufSet[0])
shoPre(preSet, plim, filld)
shoSuf(sufSet, slim, filld)