Problem 51

completed December 24, 2011

Comments

This problem required a lot of string method manipulation. After trying several combinations, I was able to work it out. Using the itertools module was also very helpful. It was able to give me the list of all possibilities with a blank tile in it.

Code

def isprime(n):
	for i in range(2, int(n**.5)+1):
		if n%i == 0: return False
	return True


def str2num(n):
	return ''.join(map(str, n))


def replace(n):
	replaceset = []
	for i in range(10):
		new = str2num(n.replace('_', str(i)))
		replaceset.append(str2num(new))
	return map(int, replaceset)


from itertools import product
choice = '0123456789_'

for n in product(choice, repeat=6):
	if '_' not in n: continue
	this = map(isprime, replace(str(str2num(n))))
	count = this.count(True)
	if count == 8: print n, count