-
Notifications
You must be signed in to change notification settings - Fork 0
/
p037.py
52 lines (39 loc) · 1.63 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
################################################################################
# P37: Truncatable primes #
################################################################################
# #
# Find the sum of the only eleven primes that are both truncatable from left #
# to right and right to left. #
# #
################################################################################
# Problem found at projecteuler.net #
# Author: ncfgrill #
################################################################################
from math import ceil, sqrt
def check_prime(num):
if num <= 3: return num > 1
elif num % 6 != 1 and num % 6 != 5: return False
for i in range(2, ceil(sqrt(num)) + 1):
if num % i == 0: return False
return True
def truncate(num):
save = num
#left-to-right
while len(num) > 0:
if not check_prime(int(num)): return False
num = num[1:]
num = save[0:-1]
# right-to-left
while len(num) > 0:
if not check_prime(int(num)): return False
num = num[0:-1]
return True
def find_primes():
primes, d, s = 0, 11, 0
while primes < 11:
if truncate(str(d)):
primes += 1
s += d
d += 2
return s
print('Sum:', find_primes())