-
Notifications
You must be signed in to change notification settings - Fork 16
/
0037-truncatable-primes.py
executable file
·93 lines (73 loc) · 2.3 KB
/
0037-truncatable-primes.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
Problem 37
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.
"""
import math
from collections import deque
from copy import copy
def enumerate_primes(n):
"""
Get all the prime numbers to n
"""
if n == 2: return [2]
elif n < 2: return []
s = range(3,n+1,2)
nroot = math.sqrt(n)
half=(n+1)/2 - 1
i = 0
m = 3
while m <= nroot:
if s[i]:
j = (m*m-3)/2
s[j] = 0
while j < half:
s[j] = 0
j += m
i = i + 1
m=2 * i + 3
return [2] + [x for x in s if x]
def the_usual_method():
start = 7
end = 800000
# get all the primes from start to end
primes = [x for x in enumerate_primes(end)]
# counting 2 as prime
truncatable_primes = []
for i in primes:
if i == 2 or i == 3 or i == 5 or i == 7:
continue
num_str = deque(str(i))
count = 1
# checking from left-to-right
l = copy(num_str)
while True:
l.popleft()
if len(l) == 0:
break
temp = ''.join(l)
num = int(temp)
if num in primes:
count = count + 1
else:
break
if count < len(num_str):
continue
count = 1
# checking from right-to-left
l = copy(num_str)
while True:
l.pop()
if len(l) == 0:
break
temp = ''.join(l)
num = int(temp)
if num in primes:
count = count + 1
else:
break
if count == len(num_str):
truncatable_primes.append(i)
return truncatable_primes
print "Answer by usual method:", the_usual_method()