Truncatable primes
Problem 37
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
from tools import rwh_primes2
def run(limit=1000000):
total = 0
primes = list(rwh_primes2(limit))
found = 0
for (i, prime) in enumerate(primes):
if found == 11:
break
if prime > 10:
s = str(prime)
good = True
c = 1
while c < len(s):
if int(s[:-c]) not in primes or int(s[c:]) not in primes:
good = False
break
c += 1
if good:
total += prime
found += 1
return total