We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.
The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.
In [1]:
import itertools
products = set()
digits = list(range(1,10))
for psize in [3,4]:
for c in itertools.permutations(digits, psize):
mult1 = int(''.join([str(d) for d in c]))
odigits = set(digits).difference(set(c))
for c2 in itertools.permutations(odigits, 5-psize):
rdigits = odigits.difference(c2)
mult2 = int(''.join([str(d) for d in c2]))
p = [int(d) for d in str(mult1*mult2)]
if len(p) == len(rdigits) and len(set(rdigits).difference(p)) == 0:
products.add(mult1*mult2)
print sum(products)
In [ ]: