Skip to content

Commit 4babe19

Browse files
suadjelilistokhos
authored andcommitted
added solution 4 for problem_20 (TheAlgorithms#1476)
1 parent 33619cc commit 4babe19

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

project_euler/problem_20/sol4.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
n! means n × (n − 1) × ... × 3 × 2 × 1
3+
4+
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
5+
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
6+
7+
Find the sum of the digits in the number 100!
8+
"""
9+
10+
11+
def solution(n):
12+
"""Returns the sum of the digits in the number 100!
13+
>>> solution(100)
14+
648
15+
>>> solution(50)
16+
216
17+
>>> solution(10)
18+
27
19+
>>> solution(5)
20+
3
21+
>>> solution(3)
22+
6
23+
>>> solution(2)
24+
2
25+
>>> solution(1)
26+
1
27+
"""
28+
fact = 1
29+
result = 0
30+
for i in range(1,n + 1):
31+
fact *= i
32+
33+
for j in str(fact):
34+
result += int(j)
35+
36+
return result
37+
38+
39+
if __name__ == "__main__":
40+
print(solution(int(input("Enter the Number: ").strip())))

0 commit comments

Comments
 (0)