-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem37.py
More file actions
42 lines (38 loc) · 1.04 KB
/
problem37.py
File metadata and controls
42 lines (38 loc) · 1.04 KB
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
"""
Trunctable primes
Project Euler Problem #37
by Muaz Siddiqui
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.
"""
from euler_helpers import timeit, is_prime
def is_trunctable(n):
if not is_prime(n):
return False
else:
temp = n//10
temp2 = n%10
dividend = 10
while temp != 0:
if not is_prime(temp):
return False
temp = temp//10
while dividend < n:
if not is_prime(temp2):
return False
dividend *=10
temp2 = n % dividend
return True
@timeit
def answer():
count = []
x = 12
while len(count) < 11:
if is_trunctable(x):
count.append(x)
x += 1
return sum(count)