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 \times 186 = 7254\), containing multiplicand, multiplier, and product is \(1\) through \(9\) pandigital.
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a \(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.
If the product has three digits, then because the two factors cannot be larger, they must also have three digits — and the product of two three-digit numbers has at least five digits, making this case impossible. If the product has five or more digits, then the divisors have four or fewer combined, so their product must be less than \(10^4\) — and so cannot have five or more digits, making this case impossible too. Therefore, the product must have four digits, and so the smaller of the two factors will have at most two. Checking each case still has to be done manually, for which I use strings, but this cuts down on a lot of time.
def p32():
prods = set()
for a in range(1, 98):
if a % 11 == 0: # repeated digits
continue
for b in range(9876 // a, a, -1):
c = a * b
s = str(a) + str(b) + str(c)
if len(s) < 9:
break
if '0' not in s and len(set(s)) == 9:
prods.add(c)
return sum(prods)