Project Euler #37: Truncatable primes

  • + 0 comments

    100 points

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    def is_prime(n):
        if n < 2:
            return False
        for i in range(2, int(n**0.5) + 1):
            if n % i == 0:
                return False
        return True
    
    def is_truncatable(n):
        s = str(n)
        for i in range(1, len(s)):
            if not is_prime(int(s[i:])) or not is_prime(int(s[:i])):
                return False
        return True
    
    limit = int(input())
    trunc_primes = []
    for p in range(10, limit):
        if is_prime(p) and is_truncatable(p):
            trunc_primes.append(p)
    print(sum(trunc_primes))