Python Implementation of Problem 38
View source code here on GitHub!
Includes
Problem Solution
Project Euler Problem 38
Once I found out where the end was, it seemed to be relatively easy
Problem:
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?
1"""
2Project Euler Problem 38
3
4Once I found out where the end was, it seemed to be relatively easy
5
6Problem:
7
8Take the number 192 and multiply it by each of 1, 2, and 3:
9
10 192 × 1 = 192
11 192 × 2 = 384
12 192 × 3 = 576
13
14By concatenating each product we get the 1 to 9 pandigital, 192384576. We will
15call 192384576 the concatenated product of 192 and (1,2,3)
16
17The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and
185, giving the pandigital, 918273645, which is the concatenated product of 9 and
19(1,2,3,4,5).
20
21What is the largest 1 to 9 pandigital 9-digit number that can be formed as the
22concatenated product of an integer with (1,2, ... , n) where n > 1?
23"""
24from functools import reduce
25from typing import Tuple
26
27from .lib.iters import digits
28
29
30def main() -> int:
31 answer: Tuple[int, ...] = (9, 1, 8, 2, 7, 3, 6, 4, 5)
32 list_of_digits = list(range(1, 10))
33 for num in range(1, 10000):
34 cur_digits = tuple(digits(num))[::-1]
35 for x in range(2, 9):
36 if len(cur_digits) > 9:
37 break
38 elif any(cur_digits.count(digit) != 1 for digit in cur_digits):
39 break
40 elif len(cur_digits) == 9 and sorted(cur_digits) == list_of_digits:
41 if cur_digits > answer:
42 answer = cur_digits
43 break
44 cur_digits = cur_digits + tuple(digits(x * num))[::-1]
45 return reduce(lambda x, y: x * 10 + y, answer)