-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem0037.py
50 lines (41 loc) · 1.63 KB
/
Problem0037.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
Enonce = """
Truncatable primes
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 EulerTools
import math
import time
def main():
print(40*"=")
print(Enonce)
print(40*"-")
Solution = set()
maxi = 100_000_000 #10_000 #1_000_000
start = time.perf_counter()
for prime in EulerTools.PrimeNumber(highest_value=maxi):
if prime < 10:
continue
prime_str = str(prime)
pl = [int(prime_str[i:]) for i in range(len(prime_str))]
pr = [int(prime_str[0:i+1]) for i in range(len(prime_str))]
is_prime = [EulerTools.isPrimeNumber(p) for p in pl+pr]
if all(is_prime):
print(f"prime={prime}")
#print(f"Solution was = {Solution}")
# Remove to low solutions
Solution = Solution.difference(pl+pr)
Solution.add(prime)
print(f"Solution is now {Solution}")
end = time.perf_counter()
print(f"{Solution} en {round(end-start,2)} secondes")
print(f"List of circular prime under {maxi} : {Solution}")
print(f"There are {len(Solution)} circular primes below {maxi}")
print(40*"-")
print(f"Solution = {Solution}")
print(40*"=")
if __name__ == "__main__":
# execute only if run as a script
main()