# Euler Problem 32¶

Back to overview.

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.

We start by thinking about how many digits are used for multiplicand/multiplier/product. Obviously, the product must have more then three digits. Can it have more then 4 digits? Obviously it can because then there are only four digits for multiplicand and multiplier which is always smaller than ten thousand. So our product must be in the form $12 \cdot 345 = 5789$. This makes the rest very easy in Python.

In [1]:

    from itertools import permutations

    def is_solution(s):
        a, b, c = int(s[0:2]), int(s[2:5]), int(s[5:])
        if a * b == c:
            return c
        a, b, c = int(s[0:1]), int(s[1:5]), int(s[5:])
        if a * b == c:
            return c
        return 0

    assert(is_solution("391867254") == 7254)
    assert(is_solution("391867245") == 0)

    s = sum(set([is_solution("".join(p)) for p in permutations("123456789")]))
    assert(s == 45228)
    s

Out[1]:

    45228