summaryrefslogtreecommitdiff
path: root/Problem37.py
blob: cd7bc98128da10d4d6ddcb85bc43b541b5fa4b78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#Completed oon 9/13/2021 14:29
import math
def isPrime(n):
    if n < 2: return False
    for x in range(2,int(math.sqrt(n)+1)):
        if n%x == 0: return False
    return True
def nextPrime(n):
    n+=1
    while isPrime(n) == False:
        n+=1
    return n
def truncatable(n):
    n = str(n)
    for x in range(0,len(n)):
        m = ""
        for y in range(0,len(n)-x):
            m += n[y]
        if isPrime(int(m)) == False: return False
    #repeat but left to right
    for x in range(0,len(n)):
        m = ""
        for y in range(x,len(n)):
            m += n[y]
        if isPrime(int(m)) == False: return False
    return True
sum,count,x = 0,0,nextPrime(7)
while count < 11:
    if truncatable(x) == True:
        sum += x
        count += 1
    x = nextPrime(x)
print("Answer is:", sum)