TheAlgorithms-Python
54 строки · 1.2 Кб
1"""
2Problem 20: https://projecteuler.net/problem=20
3
4n! means n × (n − 1) × ... × 3 × 2 × 1
5
6For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
7and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
8
9Find the sum of the digits in the number 100!
10"""
11
12
13def factorial(num: int) -> int:
14"""Find the factorial of a given number n"""
15fact = 1
16for i in range(1, num + 1):
17fact *= i
18return fact
19
20
21def split_and_add(number: int) -> int:
22"""Split number digits and add them."""
23sum_of_digits = 0
24while number > 0:
25last_digit = number % 10
26sum_of_digits += last_digit
27number = number // 10 # Removing the last_digit from the given number
28return sum_of_digits
29
30
31def solution(num: int = 100) -> int:
32"""Returns the sum of the digits in the factorial of num
33>>> solution(100)
34648
35>>> solution(50)
36216
37>>> solution(10)
3827
39>>> solution(5)
403
41>>> solution(3)
426
43>>> solution(2)
442
45>>> solution(1)
461
47"""
48nfact = factorial(num)
49result = split_and_add(nfact)
50return result
51
52
53if __name__ == "__main__":
54print(solution(int(input("Enter the Number: ").strip())))
55