Home Project Euler Teaching Projects Gallery

Problem 32: 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.


Algorthim:

We first have to think about the allocation of these numbers as in what digit product are formed what two digit. If we have 2 two digit number, then, the maximum product is 99 * 99 = 9801. But, this only contains 8 digits. So, it can't even be Pandigital. How abaout 1 digit and 4 digit number? This will work along with 2, 3, 4 spread (2 digit x 3 digit = 4 digit).

So, we have two for loops: One factor that goes from a 1 digit number to 2 digit. Another factor that goes from 3 digit number to 4 digit.

Checking if a number if pandigital is pretty simple. We caa use set() function or use double for loop.

Code:

Result:

Answer: 45228
Runtime: 383 ms