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.
This is a perfect application for a simple brute force program that iterates through the possibilities and produces a solution. The only thought of optimization was to only generate as many 4 digit products as necessary in order to keep the concatenation of i, j and p at the required 9 digits. The only combinations are a 1 digit number times a 4 digit number (lowest valid = 1234) or a 2 digit number times a 3 digit number (lowest valid = 123).
A set is used to ignore duplicate products.
Runs < 1 second in Python.
from Euler import is_pandigital p = set() for i in range(2,100): start = 1234 if i>9: start = 123 for j in range(start, 10000/i+1): if is_pandigital(str(i)+str(j)+str(i*j)): p.add(i*j) print "Answer to PE32 = ",sum(p)
- More information on the Euler module can be found on the tools page.
- This solution took 21,220 iterations.
- Of the 9 sets found, the last one was 48 × 159 = 7632