project_euler.problem_074.sol1

Project Euler Problem 74: https://projecteuler.net/problem=74

The number 145 is well known for the property that the sum of the factorial of its digits is equal to 145:

1! + 4! + 5! = 1 + 24 + 120 = 145

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:

169 → 363601 → 1454 → 169 871 → 45361 → 871 872 → 45362 → 872

It is not difficult to prove that EVERY starting number will eventually get stuck in a loop. For example,

69 → 363600 → 1454 → 169 → 363601 (→ 1454) 78 → 45360 → 871 → 45361 (→ 871) 540 → 145 (→ 145)

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.

How many chains, with a starting number below one million, contain exactly sixty non-repeating terms?

Attributes

CACHE_SUM_DIGIT_FACTORIALS

CHAIN_LENGTH_CACHE

DIGIT_FACTORIALS

Functions

chain_length(→ int)

Calculate the length of the chain of non-repeating terms starting with n.

solution(→ int)

Return the number of chains with a starting number below one million which

sum_digit_factorials(→ int)

Return the sum of the factorial of the digits of n.

Module Contents

project_euler.problem_074.sol1.chain_length(n: int, previous: set | None = None) int

Calculate the length of the chain of non-repeating terms starting with n. Previous is a set containing the previous member of the chain. >>> chain_length(10101) 11 >>> chain_length(555) 20 >>> chain_length(178924) 39

project_euler.problem_074.sol1.solution(num_terms: int = 60, max_start: int = 1000000) int

Return the number of chains with a starting number below one million which contain exactly n non-repeating terms. >>> solution(10,1000) 28

project_euler.problem_074.sol1.sum_digit_factorials(n: int) int

Return the sum of the factorial of the digits of n. >>> sum_digit_factorials(145) 145 >>> sum_digit_factorials(45361) 871 >>> sum_digit_factorials(540) 145

project_euler.problem_074.sol1.CACHE_SUM_DIGIT_FACTORIALS
project_euler.problem_074.sol1.CHAIN_LENGTH_CACHE
project_euler.problem_074.sol1.DIGIT_FACTORIALS