-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem37.py
executable file
·50 lines (44 loc) · 933 Bytes
/
problem37.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
def isPrime(num):
if num == 1:
return False
for i in xrange(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
def l2r(num):
dig = len(str(num))
for i in xrange(1,dig + 1):
if isPrime(num % 10**i) == False:
return False
break
return True
def r2l(num):
res = []
temp = num
while temp >= 1:
res.append(temp)
temp = (temp - (temp % 10)) / 10
if any(isPrime(x) == False for x in res):
return False
return True
def mark(sieve, x):
for i in xrange(x + x, len(sieve), x):
sieve[i] = False
def primeS(limit):
M = [True] * limit
for i in xrange(2, int(len(M) ** 0.5) + 1):
if M[i]:
mark(M, i)
return M
def main(limit):
Z = primeS(limit)
tot = 0
for i in xrange(10, len(Z)):
if Z[i] and r2l(i) and l2r(i):
tot += i
return tot
import time
start = time.time()
result = main(1000000)
elapsed = time.time() - start
print "result %d found in %f seconds" % (result, elapsed)