Probem 38

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

192 × 1 = 192
192 × 2 = 384
192 × 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?


In [1]:
def concatenated_product(x, n):
    return int(''.join([str(x*i) for i in range(1,n+1)]))

def is_pandigital(x):
    if x > 987654321:
        return False
    if x < 123456789:
        return False
    return set(str(x)) == {'1','2','3','4','5','6','7','8','9'}

def search(limit):
    for x in range(limit, 0, -1): # Scan from big to small since we're looking for max
        for n in range(2,10):
            y = concatenated_product(x, n)
            if is_pandigital(y):
                return y
            elif y > 987654321:
                break # try the next x

In [2]:
assert concatenated_product(192, 3) == 192384576
assert concatenated_product(9, 5) == 918273645
assert is_pandigital(123456789)
assert is_pandigital(987654321)
assert not is_pandigital(1234)

The limit has to contain unique digits because when x is that value it becomes the frist digits of the pandigital number candidate.

Next since n is at least 2, we just need to find a number x=987... such that the total number of digits in x*1 and x*2 is 9.

9876 || (9876*2)==19752 is 9 digits. So 9876 is a good limit.


In [3]:
print('Answer:', search(9876))


Answer: 932718654

In [ ]: