-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheuler037.py
71 lines (54 loc) · 1.35 KB
/
euler037.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
#!/usr/bin/env python
"""
Solution to Project Euler Problem 37
http://projecteuler.net/
by Apalala <apalala@gmail.com>
(cc) Attribution-ShareAlike
http://creativecommons.org/licenses/by-sa/3.0/
"""
from primality import is_prime, primes_upto
from memoization import memoize
TRUNCATABLE_PRIMES = {23, 37, 53, 73}
@memoize
def is_left_prime(n):
s = str(n)
while s:
n = int(s)
if not is_prime(n):
return False
elif n in TRUNCATABLE_PRIMES:
return True
s = s[1:]
return True
@memoize
def is_right_prime(n):
s = str(n)
while s:
n = int(s)
if not is_prime(n):
return False
elif n in TRUNCATABLE_PRIMES:
return True
s = s[:-1]
return True
def truncatable_primes(m):
for p in primes_upto(m):
if p < 11 or p in TRUNCATABLE_PRIMES:
continue
if is_left_prime(p) and is_right_prime(p):
TRUNCATABLE_PRIMES.add(p)
return TRUNCATABLE_PRIMES
def test():
assert is_prime(73)
assert is_prime(97)
assert is_prime(3797)
assert is_left_prime(3797)
assert is_right_prime(3797)
assert {3137, 37, 73, 797, 3797, 53, 23, 313, 317, 373} == truncatable_primes(
10 ** 4
)
def run():
print(sum(truncatable_primes(10 ** 6)))
if __name__ == "__main__":
test()
run()