view problem37.py @ 6:521bd734e291

#37 termination missing
author Dominic Cleal <dominic@computerkb.co.uk>
date Mon, 01 Dec 2008 12:44:47 +0000
parents 3271813c55ec
children ebaf68ec0784
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))
				exit(0)
	i += 2