0

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
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    if your first 2 conditions evaluate to false you will have `while false or false or len(list) < 60:` and that is `while len(list) < 60:` so it will be the only condition that finishes your while loop. i think the issue is more with the boolean logic – ussu Nov 05 '22 at 11:46

0 Answers0