|
| 1 | +""" |
| 2 | +Project Euler Problem 38: https://projecteuler.net/problem=38 |
| 3 | +
|
| 4 | +Take the number 192 and multiply it by each of 1, 2, and 3: |
| 5 | +
|
| 6 | +192 × 1 = 192 |
| 7 | +192 × 2 = 384 |
| 8 | +192 × 3 = 576 |
| 9 | +
|
| 10 | +By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call |
| 11 | +192384576 the concatenated product of 192 and (1,2,3) |
| 12 | +
|
| 13 | +The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, |
| 14 | +giving the pandigital, 918273645, which is the concatenated product of 9 and |
| 15 | +(1,2,3,4,5). |
| 16 | +
|
| 17 | +What is the largest 1 to 9 pandigital 9-digit number that can be formed as the |
| 18 | +concatenated product of an integer with (1,2, ... , n) where n > 1? |
| 19 | +
|
| 20 | +Solution: |
| 21 | +Since n>1, the largest candidate for the solution will be a concactenation of |
| 22 | +a 4-digit number and its double, a 5-digit number. |
| 23 | +Let a be the 4-digit number. |
| 24 | +a has 4 digits => 1000 <= a < 10000 |
| 25 | +2a has 5 digits => 10000 <= 2a < 100000 |
| 26 | +=> 5000 <= a < 10000 |
| 27 | +
|
| 28 | +The concatenation of a with 2a = a * 10^5 + 2a |
| 29 | +so our candidate for a given a is 100002 * a. |
| 30 | +We iterate through the search space 5000 <= a < 10000 in reverse order, |
| 31 | +calculating the candidates for each a and checking if they are 1-9 pandigital. |
| 32 | +
|
| 33 | +In case there are no 4-digit numbers that satisfy this property, we check |
| 34 | +the 3-digit numbers with a similar formula (the example a=192 gives a lower |
| 35 | +bound on the length of a): |
| 36 | +a has 3 digits, etc... |
| 37 | +=> 100 <= a < 334, candidate = a * 10^6 + 2a * 10^3 + 3a |
| 38 | + = 1002003 * a |
| 39 | +""" |
| 40 | + |
| 41 | +from typing import Union |
| 42 | + |
| 43 | + |
| 44 | +def is_9_pandigital(n: int) -> bool: |
| 45 | + """ |
| 46 | + Checks whether n is a 9-digit 1 to 9 pandigital number. |
| 47 | + >>> is_9_pandigital(12345) |
| 48 | + False |
| 49 | + >>> is_9_pandigital(156284973) |
| 50 | + True |
| 51 | + >>> is_9_pandigital(1562849733) |
| 52 | + False |
| 53 | + """ |
| 54 | + s = str(n) |
| 55 | + return len(s) == 9 and set(s) == set("123456789") |
| 56 | + |
| 57 | + |
| 58 | +def solution() -> Union[int, None]: |
| 59 | + """ |
| 60 | + Return the largest 1 to 9 pandigital 9-digital number that can be formed as the |
| 61 | + concatenated product of an integer with (1,2,...,n) where n > 1. |
| 62 | + """ |
| 63 | + for base_num in range(9999, 4999, -1): |
| 64 | + candidate = 100002 * base_num |
| 65 | + if is_9_pandigital(candidate): |
| 66 | + return candidate |
| 67 | + |
| 68 | + for base_num in range(333, 99, -1): |
| 69 | + candidate = 1002003 * base_num |
| 70 | + if is_9_pandigital(candidate): |
| 71 | + return candidate |
| 72 | + |
| 73 | + return None |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + print(f"{solution() = }") |
0 commit comments