Advertisement
makispaiktis

Project Euler 20 - Factorial Digit Sum

Aug 10th, 2020 (edited)
1,412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. def factorial(n):
  2.     if n < 0 or n != int(n):
  3.         print("Error using factorial function")
  4.         return -1000
  5.     elif n == 0:
  6.         return 1
  7.     else:
  8.         fact = list()
  9.         fact.append(1)
  10.         for i in range(1, n+2):
  11.             fact.append(i * fact[i-1])
  12.         return fact[n]
  13.  
  14. def countDigits(n):
  15.     string = str(n)
  16.     sum = 0
  17.     for i in range(len(string)):
  18.         ch = string[i]
  19.         sum += int(ch)
  20.     return sum
  21.  
  22. # MAIN FUNCTION
  23. n = 100
  24. fact = factorial(n)
  25. print(str(n) + "! = " + str(fact))
  26. print("The sum of all the digits of " + str(n) + "! is: " + str(countDigits(fact)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement