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.
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
Assume first multiplier takes $ i $ digits, second multiplier takes $ j $ digits, then product takes $ 9-i-j $ digits.
The minimum product is $ 10^{i+j-2} $, and maximum product is $ 10^{i+j} $, so we get $ i+j-2 \le 9-(i+j) \le i+j $, so $ i+j = 5 $.
Get all permutations of digits 1 through 9, and check if could get pandigital product.
from itertools import permutations
def get_number(p, slc):
return int(''.join(map(str, p[slc])))
get_number(range(1, 10), slice(0, 2))
get_number(range(1, 10), slice(5, 9))
def solve():
s = 0
pandigital_products = []
for p in permutations(range(1, 10), 9):
for i in [1, 2]:
if get_number(p, slice(5, 9)) not in pandigital_products and \
get_number(p, slice(0, i)) * get_number(p, slice(i, 5)) == get_number(p, slice(5, 9)):
pandigital_products.append(get_number(p, slice(5, 9)))
s += get_number(p, slice(5, 9))
return s, pandigital_products
solve()