TheAlgorithms-Python
39 строк · 1.1 Кб
1"""
2Project Euler Problem 80: https://projecteuler.net/problem=80
3Author: Sandeep Gupta
4Problem statement: For the first one hundred natural numbers, find the total of
5the digital sums of the first one hundred decimal digits for all the irrational
6square roots.
7Time: 5 October 2020, 18:30
8"""
9
10import decimal
11
12
13def solution() -> int:
14"""
15To evaluate the sum, Used decimal python module to calculate the decimal
16places up to 100, the most important thing would be take calculate
17a few extra places for decimal otherwise there will be rounding
18error.
19
20>>> solution()
2140886
22"""
23answer = 0
24decimal_context = decimal.Context(prec=105)
25for i in range(2, 100):
26number = decimal.Decimal(i)
27sqrt_number = number.sqrt(decimal_context)
28if len(str(sqrt_number)) > 1:
29answer += int(str(sqrt_number)[0])
30sqrt_number_str = str(sqrt_number)[2:101]
31answer += sum(int(x) for x in sqrt_number_str)
32return answer
33
34
35if __name__ == "__main__":
36import doctest
37
38doctest.testmod()
39print(f"{solution() = }")
40