Pandigital products

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.

HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

Idea

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.


In [1]:
from itertools import permutations
In [2]:
def get_number(p, slc):
    return int(''.join(map(str, p[slc])))
In [3]:
get_number(range(1, 10), slice(0, 2))
Out[3]:
12
In [4]:
get_number(range(1, 10), slice(5, 9))
Out[4]:
6789
In [5]:
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
In [6]:
solve()
Out[6]:
(45228, [5796, 5346, 4396, 7254, 6952, 7852, 7632])