I'm attempting to solve the following problem regarding digit factorial chains at https://projecteuler.net/problem=74
Right now, I am trying to create a ‘digit factorial chain’ function which will take a number n, calculate the sum of the factorial of its digits and then repeat the same steps, taking the sum as the new number. I want to either stop if the length of the chain is 60+ or if either the original n has appeared again in the chain or another number has appeared in the chain twice.
At the moment my while loop only successfully terminates upon exceeding 60, but not if the latter two conditions are met. Any ideas where I went wrong?
def sum_factorial_digits(n):
list = []
sum = 0
string = str(n)
while string not in list or n not in list or len(list) < 60:
sum = 0
string = str(string)
for x in string:
x = int(x)
sum += math.factorial(x)
string = sum
list.append(string)
return list