74.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from math import factorial
  2. def digitFactorial(n):
  3. """Takes integer and returns the sum of the factorial of its digits"""
  4. n = str(n)
  5. factorialSum = 0
  6. for digit in n:
  7. digit = int(digit)
  8. factorialSum += factorial(digit)
  9. return factorialSum
  10. def isDigitFactorial(n):
  11. """Checks if number is one of the 4 loops given in prompt, method not used in solution"""
  12. if n == 145 or n == 169 or n == 871 or n == 872:
  13. return True
  14. else:
  15. return n == digitFactorial(n)
  16. def isChainDigitFactorial(n):
  17. """Returns a boolean indicating if the number is a non-repeating digit factorial"""
  18. iterations = 1
  19. terms = [n]
  20. # Maximum of 50 iterations
  21. while iterations < 60:
  22. n = digitFactorial(n)
  23. if n in terms:
  24. return False
  25. else:
  26. terms.append(n)
  27. iterations += 1
  28. return True
  29. def main(limit):
  30. """Iterates through all integers up to limit to find non-repeating terms"""
  31. numberFactorialChains = 0
  32. for i in range(1, limit):
  33. if isChainDigitFactorial(i):
  34. numberFactorialChains += 1
  35. return numberFactorialChains
  36. print main(1000000)