# HG changeset patch # User Dominic Cleal # Date 1228135907 0 # Node ID ebaf68ec078412df09e13f0f170a457632aeaf70 # Parent ca8801ad08e93faf8ed1b3fec39203c4c90ea35c #37 updating to use primes library diff -r ca8801ad08e9 -r ebaf68ec0784 problem37.py --- 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)