Problem 32 : Pandigital products

Problem Statement

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.

Solution

from collections import Counter
def pandigital(x):
    a=str(x)
    j=Counter(a)
    val=0
    for i in '123456789':
        if i in j:
            if j[i]==1:
                val=val+1
    if val==9:
        return True
    return False

p = set()
for i in range(2,  60):
    start = 1234 if i < 10 else 123 
    for j in range(start, 10000//i):
        if pandigital(str(i) + str(j) + str(i*j)): p.add(i*j)

print(sum(p))

Output

45228