Mercurial > hg > euler
view problem37.py @ 4:3271813c55ec
#37 solution
author | Dominic Cleal <dominic@computerkb.co.uk> |
---|---|
date | Mon, 01 Dec 2008 11:55:05 +0000 |
parents | |
children | 521bd734e291 |
line wrap: on
line source
import math def matches(i, p, fn): si = str(i) while len(si) > 0: if not int(si) in p: 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)) i += 2