Skip to content

Latest commit

 

History

History
33 lines (22 loc) · 1.21 KB

32. Pandigital products.md

File metadata and controls

33 lines (22 loc) · 1.21 KB

Question

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.

Solution

There are 9 digits in total. The product can only be a 4-digit number. It is either a product of a 2-digit number and a 3-digit number or a product of a 1-digit number and a 4-digit number

from itertools import permutations

def p32():
    ans = set()
    for a, b, c, d, e, f, g, h, i in permutations(range(1, 10)):
        rhs = 1000 * f + 100 * g + 10 * h + i

        lhs = (a * 10 + b) * (c * 100 + d * 10 + e)
        if lhs == rhs: ans.add(rhs)

        lhs = a * (b * 1000 + c * 100 + d * 10 + e)
        if lhs == rhs: ans.add(rhs)
    return sum(ans)

Answer

45228