-
Notifications
You must be signed in to change notification settings - Fork 0
/
projecteuler37.py
68 lines (63 loc) · 1.54 KB
/
projecteuler37.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Python version = 2.7.1
# Platform = win32
def isprime(n):
'''check if integer n is a prime'''
# make sure n is a positive integer
n = abs(int(n))
# 0 and 1 are not primes
if n < 2:
return False
# 2 is the only even prime number
if n == 2:
return True
# all other even numbers are not primes
if not n & 1:
return False
# range starts with 3 and only needs to go up the squareroot of n
# for all odd numbers
for x in range(3, int(n ** 0.5) + 1, 2):
if n % x == 0:
return False
return True
def peel_left(i):
"""Remove digits left to right,
then check for prime number"""
x = list(str(i))
print x
for n in range(1, len(x)):
y = x[n:]
s = int(''.join(y))
print s
if isprime(s):
continue
else:
return "False"
return "True"
def peel_right(i):
""""Remove digits right to left,
then check for prime number"""
x = list(str(i))
for n in range(1, len(x)):
y = x[:(-n)]
s = int(''.join(y))
if isprime(s):
continue
else:
return "False"
return "True"
def main():
"""Main Program"""
L = []
N = int(raw_input())
for i in range(10, N+1):
if isprime(i):
if (peel_left(i) == "True") and (peel_right(i) == "True"):
L.append(i)
else:
continue
else:
continue
#print "L = ", L
print sum(L)
if __name__ == '__main__':
main()