Problem 37

Python

 1import math
 2def is_prime(num):
 3    if num == 2:
 4        return True
 5    if num == 1:
 6        return False
 7    #rule out all even numbers to cut run time
 8    if num % 2 == 0:
 9        return False
10    for i in range(2, math.ceil(math.sqrt(num)) + 1):
11        if num % i == 0:
12            return False
13    return True
14
15def is_truncatable(num):
16    check_front = str(num)
17    check_back = str(num)
18    test_is_prime = True
19    check_front = check_front[1:]
20    while test_is_prime and check_front != "":
21        test_is_prime = is_prime(int(check_front))
22        if not test_is_prime:
23            return False
24        check_front = check_front[1:]
25    if not test_is_prime:
26        return False
27    check_back = check_back[:-1]
28    while test_is_prime and check_back != "":
29        test_is_prime = is_prime(int(check_back))
30        if not test_is_prime:
31            return False
32        check_back = check_back[:-1]
33    if test_is_prime:
34        return True
35
36def main():
37    sum_num = 0
38    count = 0
39    iterator = 10
40    while count != 11 and iterator <= 1000000:
41        if is_prime(iterator):
42            if is_truncatable(iterator):
43                sum_num += iterator
44                count += 1
45        iterator += 1
46    print(sum_num)
47
48
49
50if __name__ == "__main__":main()