Truncatable primes

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.


Idea

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.


In [1]:
import math
In [2]:
heads = [2, 3, 5, 7]
tails = [1, 3, 7, 9]
In [3]:
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))
In [4]:
def is_prime_left2right(n):
    return all(map(is_prime, (n % pow(10, p) for p in range(1, len(str(n))))))
In [5]:
is_prime_left2right(3797)
Out[5]:
True
In [6]:
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
In [7]:
solve()
Out[7]:
(748317, [23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 739397])