changeset 9:ebaf68ec0784

#37 updating to use primes library
author Dominic Cleal <dominic@computerkb.co.uk>
date Mon, 01 Dec 2008 12:51:47 +0000
parents ca8801ad08e9
children 449e1d2e103c
files problem37.py
diffstat 1 files changed, 11 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/problem37.py	Mon Dec 01 12:51:34 2008 +0000
+++ b/problem37.py	Mon Dec 01 12:51:47 2008 +0000
@@ -1,32 +1,21 @@
 import math
+import primes
 
-def matches(i, p, fn):
+def matches(i, s, fn):
 	si = str(i)
 	while len(si) > 0:
-		if not int(si) in p:
+		if not s.isprime(int(si)):
 			return False
 		si = fn(si)
 	return True
 
 tr = []
-p = [ 2 ]
-i = 3
-while True:
-	isp = True
-	max = int(math.floor(math.sqrt(i)))
-	for t in p[1:]:
-		if t > max:
-			break
-		if i % t == 0:
-			isp = False
-			break
-	if isp:
-		p.append(i)
-		if i > 9 and matches(i, p, lambda s: s[1:]) and matches(i, p, lambda s: s[:-1]):
-			print "Truncatable: %d" % (i)
-			tr.append(i)
-			if len(tr) == 11:
-				print "Eleven found, sum = %d" % (sum(tr))
-				exit(0)
-	i += 2
+s = primes.testsieve()
+for i in s.sieve():
+	if i > 9 and matches(i, s, lambda s: s[1:]) and matches(i, s, lambda s: s[:-1]):
+		print "Truncatable: %d" % (i)
+		tr.append(i)
+		if len(tr) == 11:
+			print "Eleven found, sum = %d" % (sum(tr))
+			exit(0)