Python: Factorial Digit Sum
Python: Factorial Digit Sum
Problem 20 @ projectEuler
The Task: The task in projectEuler P20 is to get the sum of the digits in the number Factorial of 100!
Factorial NdefinitionThe factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 10! = 10 × 9 × … × 3 × 2 × 1 = 3628800.
Problem 20 is another easy problem in projectEuler, and we will write two functions to solve it. First one is a Factorial_digit_sum this one will return the factorial of a number. The second function will calculate the sum of all digits in a number N and we will call it sum_of_digits.
Clarification As long as i just start solving or posting my answers to projectEuler portal, i am selecting problems and not going through them in sequence, that’s way my posts are jumps between problems, so if i am posting the code to solve problem 144 (for example) that does’t meaning that i solve all problems before it.
print of solved screen:
The Code:
#Python: Factorial Digit Sum
#Problem No.20 on projectEuler
def Factorial_digit_sum(num):
if (num == 0) :
return 1
else:
return num * Factorial_digit_sum(num-1)
num=100
fact =Factorial_digit_sum(100)
print fact,’is the Factorial of {}.’.format(num)
def sum_of_digits(dig):
t = 0
for each in dig:
t = t + int(each)
print ‘\nThe sum of your number is’,t
sum_of_digits(str(tot1))