-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler37.py
57 lines (42 loc) · 914 Bytes
/
euler37.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
from math import ceil, log10
def isPrime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
for x in range(3, int(n ** 0.5) + 1, 2):
if n % x == 0:
return False
return True
def truncated_right(n):
l = []
while n > 10:
n /= 10
l.append(n)
return l
def truncated_left(n):
l = []
i = pow(10, ceil(log10(n)) - 1)
while n % i > 0:
n %= i
i /= 10
l.append(int(n))
return l
limit = int(1e6)
primes = (x for x in range(11, limit + 1) if isPrime(x))
s = 0
found = 0
for p in primes:
trunks = truncated_right(p) + truncated_left(p)
truncable = all(isPrime(h) for h in trunks)
if truncable:
found += 1
s += p
print(p)
# it said there are only 11 such primes
if found == 11:
break
print("")
print(s)