# HG changeset patch # User Dominic Cleal # Date 1228132505 0 # Node ID 3271813c55ec4b6782006036f31c25499f24fb45 # Parent b94533a8c529aaec08fbdd05f68b634e8adc4ae5 #37 solution diff -r b94533a8c529 -r 3271813c55ec problem37.py --- /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 +