-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.py
37 lines (26 loc) · 784 Bytes
/
20.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'''
Author: Michael Sherif Naguib
Date: May 7, 2019
@: University of Tulsa
Question #20:
n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!
'''
if __name__ == "__main__":
'''
my approach: iteration ... i think recursion might exceed pythons recursive depth and it will use a lot of memory
'''
#Find the factorial
num=1
for i in range(2,101):#note bound is exclusive.. so it is 2-100
num = num*i
#convert to a string
strNum=str(num)
#Sum
digitSum=0
for d in strNum:
digitSum+= int(d)
#print
print(digitSum)