TheAlgorithms-Python

Форк
0
54 строки · 1.2 Кб
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 factorial(num: int) -> int:
14
    """Find the factorial of a given number n"""
15
    fact = 1
16
    for i in range(1, num + 1):
17
        fact *= i
18
    return fact
19

20

21
def split_and_add(number: int) -> int:
22
    """Split number digits and add them."""
23
    sum_of_digits = 0
24
    while number > 0:
25
        last_digit = number % 10
26
        sum_of_digits += last_digit
27
        number = number // 10  # Removing the last_digit from the given number
28
    return sum_of_digits
29

30

31
def solution(num: int = 100) -> int:
32
    """Returns the sum of the digits in the factorial of num
33
    >>> solution(100)
34
    648
35
    >>> solution(50)
36
    216
37
    >>> solution(10)
38
    27
39
    >>> solution(5)
40
    3
41
    >>> solution(3)
42
    6
43
    >>> solution(2)
44
    2
45
    >>> solution(1)
46
    1
47
    """
48
    nfact = factorial(num)
49
    result = split_and_add(nfact)
50
    return result
51

52

53
if __name__ == "__main__":
54
    print(solution(int(input("Enter the Number: ").strip())))
55

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

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

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

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