TheAlgorithms-Python

Форк
0
39 строк · 1.1 Кб
1
"""
2
Project Euler Problem 80: https://projecteuler.net/problem=80
3
Author: Sandeep Gupta
4
Problem statement: For the first one hundred natural numbers, find the total of
5
the digital sums of the first one hundred decimal digits for all the irrational
6
square roots.
7
Time: 5 October 2020, 18:30
8
"""
9

10
import decimal
11

12

13
def solution() -> int:
14
    """
15
    To evaluate the sum, Used decimal python module to calculate the decimal
16
    places up to 100, the most important thing would be take calculate
17
    a few extra places for decimal otherwise there will be rounding
18
    error.
19

20
    >>> solution()
21
    40886
22
    """
23
    answer = 0
24
    decimal_context = decimal.Context(prec=105)
25
    for i in range(2, 100):
26
        number = decimal.Decimal(i)
27
        sqrt_number = number.sqrt(decimal_context)
28
        if len(str(sqrt_number)) > 1:
29
            answer += int(str(sqrt_number)[0])
30
            sqrt_number_str = str(sqrt_number)[2:101]
31
            answer += sum(int(x) for x in sqrt_number_str)
32
    return answer
33

34

35
if __name__ == "__main__":
36
    import doctest
37

38
    doctest.testmod()
39
    print(f"{solution() = }")
40

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

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

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

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