-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_37.py
More file actions
38 lines (33 loc) · 897 Bytes
/
problem_37.py
File metadata and controls
38 lines (33 loc) · 897 Bytes
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
# Truncatable primes
# problem 37
import math
def is_prime(num):
if num == 2:
return True
if num == 1 or num % 2 == 0:
return False
for n in range(3,int(math.sqrt(num)+1),2):
if num % n == 0:
return False
return True
def is_truncatable_prime_left(string):
for i in range(len(string)):
if not is_prime(int(string[:i+1])):
return False
return True
def is_truncatable_prime_right(string):
for i in range(len(string)):
if not is_prime(int(string[i:])):
return False
return True
n = 10
count = 0
result = 0
while count < 11:
n += 1
if is_prime(n):
num_str = str(n)
if is_truncatable_prime_left(num_str) and is_truncatable_prime_right(num_str):
result += n
count += 1
print(result)