-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem037.py
More file actions
48 lines (40 loc) · 1.01 KB
/
problem037.py
File metadata and controls
48 lines (40 loc) · 1.01 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
43
44
45
46
47
48
# Copyright 2018 Keval Khara kevalk@bu.edu
'''
We use regular expressions to increase the efficiency of
our code since the numbers with even digits can be
skipped as we know they won't be prime while truncating
the digits
'''
import re
from math import sqrt
def is_prime(n):
'''Check if the number is prime'''
n = int(n)
if n <= 1:
return False
if n <= 3:
return True
if n%2==0 or n%3 == 0:
return False
f = 5
while f <= int(sqrt(n)):
if n%f == 0 or n%(f+2) == 0:
return False
f+= 6
return True
def is_trunc(n):
'''Check if the truncated numbers are prime'''
for d in range(1, len(str(n))):
if not is_prime(str(n)[d:]) or not is_prime(str(n)[:d]):
return False
return True
n = 11
f = 1
ans = []
while len(ans) < 11:
n += 3-f # fast count for primes
f = -f
if not (n > 100 and re.search('[245680]', str(n))):
if is_prime(n) and is_trunc(n):
ans.append(n)
print(sum(ans))