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.

     

    Answer:
    748317

     

    #!/usr/bin/env python
    import math

    def sieve_method (n ):
    prime_data = [x for x in xrange (n + 1 ) ]
    for i in xrange ( 2, int ( math. sqrt (n ) + 1 ) ):
    start = i ** 2
    step = i
    prime_data [start::step ] = ( (n - start ) / step + 1 ) * [ 0 ]
    prime_data [ 1 ] = 0
    return prime_data

    def is_truncateble (n ):
    i, j = str (n ), str (n )
    while len (i ) > 1: # left to right
    i = i [: -1 ]
    if prime_data [ int (i ) ] == 0:
    return False
    while len (j ) > 1: # rught to left
    j = j [ 1: ]
    if prime_data [ int (j ) ] == 0:
    return False
    return True

    ilist= [ ]
    prime_data = sieve_method ( 1000000 )
    for n in prime_data:
    if n and is_truncateble (n ):
    ilist. append (n )
    print sum (ilist [ 4: 15 ] ) # 2, 3, 5, 7 not contain