-
Notifications
You must be signed in to change notification settings - Fork 0
/
37TruncatablePrimes.py
42 lines (31 loc) · 1012 Bytes
/
37TruncatablePrimes.py
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
35
36
37
38
39
40
41
42
##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. ##
from prime import Prime
p = Prime()
def truncatableprime(n):
## from left to right ##
for i in range(len(str(n))):
if not p.isprime(int(str(n)[i:])):
return False
## from right to left
tmp = n
while tmp>0:
if not p.isprime(tmp):
return False
tmp = tmp/10
return True
count = 1
n = 10
sumofTP = 0
while count <= 11:
n = p.getnextprime(n)
if truncatableprime(n):
print n
sumofTP += n
count += 1
print "sum is ", sumofTP
##Ans: 748317 ##