2020-11-01 13:12:21 +00:00
|
|
|
"""
|
2022-05-12 03:48:04 +00:00
|
|
|
Project Euler Problem 074: https://projecteuler.net/problem=74
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
The number 145 is well known for the property that the sum of the factorial of its
|
|
|
|
digits is equal to 145:
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
1! + 4! + 5! = 1 + 24 + 120 = 145
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
Perhaps less well known is 169, in that it produces the longest chain of numbers that
|
|
|
|
link back to 169; it turns out that there are only three such loops that exist:
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
169 → 363601 → 1454 → 169
|
|
|
|
871 → 45361 → 871
|
|
|
|
872 → 45362 → 872
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
It is not difficult to prove that EVERY starting number will eventually get stuck in a
|
|
|
|
loop. For example,
|
2020-11-20 04:14:08 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
69 → 363600 → 1454 → 169 → 363601 (→ 1454)
|
|
|
|
78 → 45360 → 871 → 45361 (→ 871)
|
|
|
|
540 → 145 (→ 145)
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
Starting with 69 produces a chain of five non-repeating terms, but the longest
|
|
|
|
non-repeating chain with a starting number below one million is sixty terms.
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
How many chains, with a starting number below one million, contain exactly sixty
|
|
|
|
non-repeating terms?
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
Solution approach:
|
|
|
|
This solution simply consists in a loop that generates the chains of non repeating
|
|
|
|
items using the cached sizes of the previous chains.
|
|
|
|
The generation of the chain stops before a repeating item or if the size of the chain
|
|
|
|
is greater then the desired one.
|
|
|
|
After generating each chain, the length is checked and the counter increases.
|
|
|
|
"""
|
2024-03-13 06:52:41 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
from math import factorial
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
DIGIT_FACTORIAL: dict[str, int] = {str(digit): factorial(digit) for digit in range(10)}
|
2020-11-20 04:14:08 +00:00
|
|
|
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
def digit_factorial_sum(number: int) -> int:
|
|
|
|
"""
|
|
|
|
Function to perform the sum of the factorial of all the digits in number
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
>>> digit_factorial_sum(69.0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TypeError: Parameter number must be int
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
>>> digit_factorial_sum(-1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Parameter number must be greater than or equal to 0
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
>>> digit_factorial_sum(0)
|
|
|
|
1
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
>>> digit_factorial_sum(69)
|
2020-11-01 13:12:21 +00:00
|
|
|
363600
|
|
|
|
"""
|
2022-05-12 03:48:04 +00:00
|
|
|
if not isinstance(number, int):
|
|
|
|
raise TypeError("Parameter number must be int")
|
|
|
|
|
|
|
|
if number < 0:
|
|
|
|
raise ValueError("Parameter number must be greater than or equal to 0")
|
|
|
|
|
|
|
|
# Converts number in string to iterate on its digits and adds its factorial.
|
|
|
|
return sum(DIGIT_FACTORIAL[digit] for digit in str(number))
|
2020-11-01 13:12:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def solution(chain_length: int = 60, number_limit: int = 1000000) -> int:
|
|
|
|
"""
|
2022-05-12 03:48:04 +00:00
|
|
|
Returns the number of numbers below number_limit that produce chains with exactly
|
|
|
|
chain_length non repeating elements.
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
>>> solution(10.0, 1000)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TypeError: Parameters chain_length and number_limit must be int
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
>>> solution(10, 1000.0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TypeError: Parameters chain_length and number_limit must be int
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
>>> solution(0, 1000)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Parameters chain_length and number_limit must be greater than 0
|
2020-11-20 04:14:08 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
>>> solution(10, 0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Parameters chain_length and number_limit must be greater than 0
|
2020-11-20 04:14:08 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
>>> solution(10, 1000)
|
|
|
|
26
|
|
|
|
"""
|
2020-11-20 04:14:08 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
if not isinstance(chain_length, int) or not isinstance(number_limit, int):
|
|
|
|
raise TypeError("Parameters chain_length and number_limit must be int")
|
2020-11-20 04:14:08 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
if chain_length <= 0 or number_limit <= 0:
|
|
|
|
raise ValueError(
|
|
|
|
"Parameters chain_length and number_limit must be greater than 0"
|
|
|
|
)
|
2020-11-01 13:12:21 +00:00
|
|
|
|
2022-05-12 03:48:04 +00:00
|
|
|
# the counter for the chains with the exact desired length
|
|
|
|
chains_counter = 0
|
|
|
|
# the cached sizes of the previous chains
|
|
|
|
chain_sets_lengths: dict[int, int] = {}
|
|
|
|
|
|
|
|
for start_chain_element in range(1, number_limit):
|
|
|
|
# The temporary set will contain the elements of the chain
|
|
|
|
chain_set = set()
|
|
|
|
chain_set_length = 0
|
|
|
|
|
|
|
|
# Stop computing the chain when you find a cached size, a repeating item or the
|
|
|
|
# length is greater then the desired one.
|
|
|
|
chain_element = start_chain_element
|
|
|
|
while (
|
|
|
|
chain_element not in chain_sets_lengths
|
|
|
|
and chain_element not in chain_set
|
|
|
|
and chain_set_length <= chain_length
|
|
|
|
):
|
|
|
|
chain_set.add(chain_element)
|
|
|
|
chain_set_length += 1
|
|
|
|
chain_element = digit_factorial_sum(chain_element)
|
|
|
|
|
|
|
|
if chain_element in chain_sets_lengths:
|
|
|
|
chain_set_length += chain_sets_lengths[chain_element]
|
|
|
|
|
|
|
|
chain_sets_lengths[start_chain_element] = chain_set_length
|
|
|
|
|
|
|
|
# If chain contains the exact amount of elements increase the counter
|
|
|
|
if chain_set_length == chain_length:
|
|
|
|
chains_counter += 1
|
|
|
|
|
|
|
|
return chains_counter
|
2020-11-01 13:12:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|
|
|
|
print(f"{solution()}")
|