# Euler Problem 38¶

Take the number 192 and multiply it by each of 1, 2, and 3:

$192 \times 1 = 192$

$192 \times 2 = 384$

$192 \times 3 = 576$

By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)

The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).

What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n > 1?

Let's start by implementing a function which can calculate the concatenated product of a number based on a given $n$.

In [1]:

    def get_concatenated_product_of(number, n):
        number = int(number)
        return "".join(str(number * (i + 1)) for i in range(n))

    assert(get_concatenated_product_of(9, 5) == "918273645")
    assert(get_concatenated_product_of(192, 3) == "192384576")

Next we need a function that can check if a number is pandigital for the numbers from 1 to 9. The idea is that we generate a validation array for the numbers 1 to 9. Each number maps to a field in the array and we initialize the array with zeros. We then iterate over the input number and increment the respective validation field by one. Afterwards we check if each field in the validation array is one in which case the number is n-pandigital. Otherwise, it is not.

In [2]:

    def is_pandigital(number, n=9):
        number_str = str(number)
        validation_array = [0 for _ in range(n)]
        if "0" in number_str:
            return False
        for digit in number_str:
            validation_array[ord(digit) - 49] += 1
        for n in validation_array:
            if n != 1:
                return False
        return True

    assert(is_pandigital("012345678") == False)
    assert(is_pandigital("123") == False)
    assert(is_pandigital("9") == False)
    assert(is_pandigital("123568") == False)
    assert(is_pandigital("987", 9) == False)
    assert(is_pandigital("918273645"))

This example shows how important it is to add little tests even for simple programs like this. The first example is certainly not 1 to 9 pandigital, but because of negative array indexing in Python it qualifies as True. We solved this problem by adding a simple check for a "0" in the number_str.

The next step is to check whether a number has the potential to become a 1 to 9 pandigital. The algorithm is essentially the same except this time we return False only if a field in the validation array is greater than 1. This means at least one digit occurs multiple times. Otherwise, the array has still potential to become a pandigital number.

In [3]:

    def could_be_pandigital(number, n=9):
        number_str = str(number)
        validation_array = [0 for _ in range(n)]
        if "0" in number_str:
            return False
        for digit in number_str:
            validation_array[ord(digit) - 49] += 1
        for n in validation_array:
            if n > 1:
                return False
        return True

    assert(could_be_pandigital("012345678") == False)
    assert(could_be_pandigital("123") == True)
    assert(could_be_pandigital("9") == True)
    assert(could_be_pandigital("123568") == True)
    assert(could_be_pandigital("987", 9) == True)
    assert(could_be_pandigital("918273645"))
    assert(could_be_pandigital("98233") == False)
    assert(could_be_pandigital("1223") == False)

The next function we want is one which checks if a certain number can actually create a concatenated pandigital product of an integer with (1,2, ... , n) where n > 1. For this function we can use all function which we have implemented to this point. We take a number and do a simple brute force starting with n = 2. We return False once the concatenated product cannot be a pandigital anymore. We use the two given examples as tests.

In [4]:

    def can_build_concatenated_pandigital_product(number):
        number_str = str(number)
        for n in range(2, 9):
            concatenated_product = get_concatenated_product_of(number_str, n)
            if is_pandigital(concatenated_product):
                return concatenated_product
            if could_be_pandigital(concatenated_product) == False:
                return False
        raise Exception("If we got here we have a bug.")

    assert(can_build_concatenated_pandigital_product(9) == "918273645")
    assert(can_build_concatenated_pandigital_product(192) == "192384576")

From here on it is just a matter of bruteforcing. We know that the base number of a higher concatenated pandigital product has to start with a 9. Also we know that the base number cannot have more than 5 digits because $10000 \times 1, 10000 \times 2 = 1000020000" has already more digits than a potential solution. This means we create a list of possible solutions first.

In [5]:

    possible_solutions = [i for i in range(1, 10000)
                          if str(i).startswith("9")
                          if not "0" in str(i)
                          if could_be_pandigital(i)]

    print("Number of possible solutions: {}".format(len(possible_solutions)))

    Number of possible solutions: 401

Since this number is actually small enough we filter all base numbers which can produce a concatenated pandigital product.

In [6]:

    possible_solutions = [s for s in possible_solutions
                          if can_build_concatenated_pandigital_product(s)]

    print("Number of possible solutions: {}".format(len(possible_solutions)))

    Number of possible solutions: 4

Finally, calculate the actual product and print the highest one.

In [7]:

    s = max(map(int, map(can_build_concatenated_pandigital_product, possible_solutions)))
    assert(s == 932718654)
    print(s)

    932718654