-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem20.py
More file actions
44 lines (31 loc) · 920 Bytes
/
Problem20.py
File metadata and controls
44 lines (31 loc) · 920 Bytes
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
38
39
40
41
42
43
44
# Code by @AmirMotefaker
# projecteuler.net
# https://projecteuler.net/problem=20
# Factorial digit sum
# Problem 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!
import time
start_time = time.time() #Time at the start of program execution
def factorial(n):
if n == 0:
return(1)
elif n == 1:
return(1)
else:
return(n*factorial(n-1))
def main():
x = 100
answer = factorial(x)
digits = list(str(answer))
sum_of_digits = 0
for digit in digits:
sum_of_digits += int(digit)
print(sum_of_digits)
main()
# print("100! =", factorial(100))
end_time = time.time() #Time at the end of execution
print ("Time of program execution:", (end_time - start_time)) # Time of program execution
### Answer: 648