Problem 32

completed December 11, 2011

Comments

This one was also pretty simple. The one catch was compensating for different forms of the numbers. The digits aren’t always 2+3+4, so I had to also write into the script to check for ones that are 1+4+4 and 2+2+5. That captured all of them.

Code

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

from itertools import permutations

prodlist = []
sum = 0
for r in permutations(range(1, 10)):
	multiplicand = str2num(r[0:2])
	multiplier = str2num(r[2:5])
	product = str2num(r[5:9])
	if (multiplicand * multiplier == product) and (product not in prodlist):
		prodlist.append(product)
		sum += product
		print product

for r in permutations(range(1, 10)):
	multiplicand = str2num(r[0:1])
	multiplier = str2num(r[1:5])
	product = str2num(r[5:9])
	if (multiplicand * multiplier == product) and (product not in prodlist):
		prodlist.append(product)
		sum += product
		print product
		
for r in permutations(range(1, 10)):
	multiplicand = str2num(r[0:2])
	multiplier = str2num(r[2:4])
	product = str2num(r[4:9])
	if (multiplicand * multiplier == product) and (product not in prodlist):
		prodlist.append(product)
		sum += product
		print product

print sum, 'here'