princeofsudan

Project Euler#20 Factorial digit sum

Mar 4th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. #n! means n × (n − 1) × ... × 3 × 2 × 1
  2.  
  3. #For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
  4. #and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
  5.  
  6. #Find the sum of the digits in the number 100!
  7.  
  8. def fact(n):
  9.     if n == 1:
  10.         return n
  11.     return n*fact(n-1)
  12.  
  13. digitsString = str(fact(100))
  14. print digitsString
  15. acc = 0
  16.  
  17. for i in range(len(digitsString)):
  18.     acc = acc + int(digitsString[i])
  19.  
  20. print acc
Advertisement
Add Comment
Please, Sign In to add comment