-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp037.py
52 lines (37 loc) · 1.06 KB
/
p037.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
"""
Truncatable primes
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.
https://projecteuler.net/problem=37
"""
from eulerlib import Crible
crible = Crible(1000000)
nb = 0
somme = 0
for p in crible.liste():
if p < 10:
continue
ko = False
gauche = p
droite = 0
i = 0
while gauche >= 10 and not ko:
gauche, r = divmod(gauche, 10)
if not crible.est_premier(gauche):
ko = True
droite = droite + r * 10 ** i
if not crible.est_premier(droite):
ko = True
i += 1
if not ko:
nb += 1
somme += p
# print(nb, p)
if nb == 11:
# print("fin")
break
print(somme)