|
| 1 | +""" |
| 2 | +Project Euler Problem 74: https://projecteuler.net/problem=74 |
| 3 | +
|
| 4 | +The number 145 is well known for the property that the sum of the factorial of its |
| 5 | +digits is equal to 145: |
| 6 | +
|
| 7 | +1! + 4! + 5! = 1 + 24 + 120 = 145 |
| 8 | +
|
| 9 | +Perhaps less well known is 169, in that it produces the longest chain of numbers that |
| 10 | +link back to 169; it turns out that there are only three such loops that exist: |
| 11 | +
|
| 12 | +169 → 363601 → 1454 → 169 |
| 13 | +871 → 45361 → 871 |
| 14 | +872 → 45362 → 872 |
| 15 | +
|
| 16 | +It is not difficult to prove that EVERY starting number will eventually get stuck in |
| 17 | +a loop. For example, |
| 18 | +
|
| 19 | +69 → 363600 → 1454 → 169 → 363601 (→ 1454) |
| 20 | +78 → 45360 → 871 → 45361 (→ 871) |
| 21 | +540 → 145 (→ 145) |
| 22 | +
|
| 23 | +Starting with 69 produces a chain of five non-repeating terms, but the longest |
| 24 | +non-repeating chain with a starting number below one million is sixty terms. |
| 25 | +
|
| 26 | +How many chains, with a starting number below one million, contain exactly sixty |
| 27 | +non-repeating terms? |
| 28 | +""" |
| 29 | + |
| 30 | + |
| 31 | +DIGIT_FACTORIALS = { |
| 32 | + "0": 1, |
| 33 | + "1": 1, |
| 34 | + "2": 2, |
| 35 | + "3": 6, |
| 36 | + "4": 24, |
| 37 | + "5": 120, |
| 38 | + "6": 720, |
| 39 | + "7": 5040, |
| 40 | + "8": 40320, |
| 41 | + "9": 362880, |
| 42 | +} |
| 43 | + |
| 44 | +CACHE_SUM_DIGIT_FACTORIALS = {145: 145} |
| 45 | + |
| 46 | +CHAIN_LENGTH_CACHE = { |
| 47 | + 145: 0, |
| 48 | + 169: 3, |
| 49 | + 36301: 3, |
| 50 | + 1454: 3, |
| 51 | + 871: 2, |
| 52 | + 45361: 2, |
| 53 | + 872: 2, |
| 54 | + 45361: 2, |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +def sum_digit_factorials(n: int) -> int: |
| 59 | + """ |
| 60 | + Return the sum of the factorial of the digits of n. |
| 61 | + >>> sum_digit_factorials(145) |
| 62 | + 145 |
| 63 | + >>> sum_digit_factorials(45361) |
| 64 | + 871 |
| 65 | + >>> sum_digit_factorials(540) |
| 66 | + 145 |
| 67 | + """ |
| 68 | + if n in CACHE_SUM_DIGIT_FACTORIALS: |
| 69 | + return CACHE_SUM_DIGIT_FACTORIALS[n] |
| 70 | + ret = sum([DIGIT_FACTORIALS[let] for let in str(n)]) |
| 71 | + CACHE_SUM_DIGIT_FACTORIALS[n] = ret |
| 72 | + return ret |
| 73 | + |
| 74 | + |
| 75 | +def chain_length(n: int, previous: set = None) -> int: |
| 76 | + """ |
| 77 | + Calculate the length of the chain of non-repeating terms starting with n. |
| 78 | + Previous is a set containing the previous member of the chain. |
| 79 | + >>> chain_length(10101) |
| 80 | + 11 |
| 81 | + >>> chain_length(555) |
| 82 | + 20 |
| 83 | + >>> chain_length(178924) |
| 84 | + 39 |
| 85 | + """ |
| 86 | + previous = previous or set() |
| 87 | + if n in CHAIN_LENGTH_CACHE: |
| 88 | + return CHAIN_LENGTH_CACHE[n] |
| 89 | + next_number = sum_digit_factorials(n) |
| 90 | + if next_number in previous: |
| 91 | + CHAIN_LENGTH_CACHE[n] = 0 |
| 92 | + return 0 |
| 93 | + else: |
| 94 | + previous.add(n) |
| 95 | + ret = 1 + chain_length(next_number, previous) |
| 96 | + CHAIN_LENGTH_CACHE[n] = ret |
| 97 | + return ret |
| 98 | + |
| 99 | + |
| 100 | +def solution(num_terms: int = 60, max_start: int = 1000000) -> int: |
| 101 | + """ |
| 102 | + Return the number of chains with a starting number below one million which |
| 103 | + contain exactly n non-repeating terms. |
| 104 | + >>> solution(10,1000) |
| 105 | + 28 |
| 106 | + """ |
| 107 | + return sum(1 for i in range(1, max_start) if chain_length(i) == num_terms) |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + print(f"{solution() = }") |
0 commit comments