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.
We can generate candidate numbers by $ prime * 10 + t, t \in [1, 3, 7, 9] $, since if we remove digits from right to left, it should remain prime.
Then we can check these candidates if remain prime when remove digits from left to right.
import math
heads = [2, 3, 5, 7]
tails = [1, 3, 7, 9]
def is_prime(n):
if (n % 2 == 0 and n > 2) or n < 2:
return False
return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))
def is_prime_left2right(n):
return all(map(is_prime, (n % pow(10, p) for p in range(1, len(str(n))))))
is_prime_left2right(3797)
def solve():
results = []
candidates = heads[:]
while len(results) < 11:
c = candidates.pop(0)
for t in tails:
new_n = c * 10 + t
if is_prime(new_n):
candidates.append(new_n)
if is_prime_left2right(new_n):
results.append(new_n)
return sum(results), results
solve()