princeofsudan

ProjectEuler#34 Digit factorials

Mar 20th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. #145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
  2.  
  3. #Find the sum of all numbers which are equal to the sum of the factorial of
  4. #their digits.
  5.  
  6. #Note: as 1! = 1 and 2! = 2 are not sums they are not included.
  7.  
  8.  
  9. #A factorion is a natural number that equals the sum of the
  10. #factorials of its decimal digits.
  11. #Upper Bound: 1,854,721
  12. #Source: http://en.wikipedia.org/wiki/Factorion
  13.  
  14. def fact(n):
  15.     if n == 1 or n == 0:
  16.         return 1
  17.     return n*fact(n-1)
  18.  
  19. def digitsFacSum(n):
  20.     digitsStr = str(n)
  21.     count = 0
  22.     for i in digitsStr:
  23.         count += fact(int(i))
  24.     return count
  25.  
  26. limit = 1854721
  27. count = 0
  28. for i in range(3,limit+1):
  29.     if digitsFacSum(i) == i:
  30.         count += i
  31. print count
Advertisement
Add Comment
Please, Sign In to add comment