Project Euler Problem 032

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.
HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

Solution

The key to solve this problem is using a language like Python and determining when to stop processing.

When do we have to stop? Well, in order not to repeat multiplications I always use that the multiplier
is greater than the multiplicand. We will increase the multiplicand until the length of the string
"multiplicand/multiplier/product" is greater or equal 10, just then we increase the multiplicand.
And we stop the algorithm when the string "multiplicand/multiplicand/multiplicand * multiplicand" is
greater or equal to 10.

from CommonFunctions import is_pandigital
 
if __name__ == '__main__':
    result = set()
    x = 2
    while len(str(x) * 2 + str(x ** 2)) <= 9:
        y = x + 1
        while len(str(x) + str(y) + str(x * y)) <= 9:
            if (x * y) not in result and is_pandigital(str(x) + str(y) + str(x * y)):
                result.add(x * y)
            y += 1
        x += 1
    result = sum(result)
    print("The result is:", result)

The Python file is available for download here.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License