Mercurial > hg > euler
changeset 4:3271813c55ec
#37 solution
author | Dominic Cleal <dominic@computerkb.co.uk> |
---|---|
date | Mon, 01 Dec 2008 11:55:05 +0000 |
parents | b94533a8c529 |
children | d1dcb923476e |
files | problem37.py |
diffstat | 1 files changed, 31 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/problem37.py Mon Dec 01 11:55:05 2008 +0000 @@ -0,0 +1,31 @@ +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 +