TheAlgorithms-Python

Форк
0
42 строки · 827.0 Байт
1
"""
2
Problem 20: https://projecteuler.net/problem=20
3

4
n! means n × (n − 1) × ... × 3 × 2 × 1
5

6
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
7
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
8

9
Find the sum of the digits in the number 100!
10
"""
11

12

13
def solution(num: int = 100) -> int:
14
    """Returns the sum of the digits in the factorial of num
15
    >>> solution(100)
16
    648
17
    >>> solution(50)
18
    216
19
    >>> solution(10)
20
    27
21
    >>> solution(5)
22
    3
23
    >>> solution(3)
24
    6
25
    >>> solution(2)
26
    2
27
    >>> solution(1)
28
    1
29
    """
30
    fact = 1
31
    result = 0
32
    for i in range(1, num + 1):
33
        fact *= i
34

35
    for j in str(fact):
36
        result += int(j)
37

38
    return result
39

40

41
if __name__ == "__main__":
42
    print(solution(int(input("Enter the Number: ").strip())))
43

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.