view problem49.py @ 14:49c96972949d default tip

#50 rewrite of solution
author Dominic Cleal <dominic@computerkb.co.uk>
date Mon, 01 Dec 2008 19:11:55 +0000
parents 0e08f4decf67
children
line wrap: on
line source

import math

def same_digits(x, y):
	lx = [c for c in str(x)]
	lx.sort()
	ly = [c for c in str(y)]
	ly.sort()
	# print "%s ~~ %s" % (lx, ly)
	return lx == ly

def is_prime_int(x):
	for t in range(3, int(math.floor(math.sqrt(x)))):
		if x % t == 0:
			return False
	return True

cache = { }
def is_prime(x):
	if x % 2 == 0:
		return False
	if x in cache:
		return cache[x]
	r = is_prime_int(x)
	cache[x] = r
	return r

for x in range(1000, 9999):
	for y in range(1000, 9999):
		(a, b, c) = (x, x+y, x+2*y)
		if c > 9999:
			break
		if not is_prime(a) or not is_prime(b) or not is_prime(c):
			continue
		if not same_digits(a, b) or not same_digits(b, c):
			continue
		print "a=%d, b=%d, c=%d" % (a, b, c)