from lib_prime import primes


def get_truncatable_numbers(n):
    s = str(n)
    ts = []
    for i in range(1, len(s)):
        ts.append(int(s[:i]))
        ts.append(int(s[-i:]))
    return ts


def euler_037():
    ps = primes(1000000)
    ps_set = set(ps)
    s = []
    for p in ps[4:]:
        for t in get_truncatable_numbers(p):
            if t not in ps_set:
                break
        else:
            s.append(p)
    return sum(s)


if __name__ == "__main__":
    print("e037.py: " + str(euler_037()))
    assert(euler_037() == 748317)
