Python/project_euler/problem_080/sol1.py
Joyce bcfca67faa
[mypy] fix type annotations for all Project Euler problems (#4747)
* [mypy] fix type annotations for problem003/sol1 and problem003/sol3

* [mypy] fix type annotations for project euler problem007/sol2

* [mypy] fix type annotations for project euler problem008/sol2

* [mypy] fix type annotations for project euler problem009/sol1

* [mypy] fix type annotations for project euler problem014/sol1

* [mypy] fix type annotations for project euler problem 025/sol2

* [mypy] fix type annotations for project euler problem026/sol1.py

* [mypy] fix type annotations for project euler problem037/sol1

* [mypy] fix type annotations for project euler problem044/sol1

* [mypy] fix type annotations for project euler problem046/sol1

* [mypy] fix type annotations for project euler problem051/sol1

* [mypy] fix type annotations for project euler problem074/sol2

* [mypy] fix type annotations for project euler problem080/sol1

* [mypy] fix type annotations for project euler problem099/sol1

* [mypy] fix type annotations for project euler problem101/sol1

* [mypy] fix type annotations for project euler problem188/sol1

* [mypy] fix type annotations for project euler problem191/sol1

* [mypy] fix type annotations for project euler problem207/sol1

* [mypy] fix type annotations for project euler problem551/sol1
2021-10-12 00:33:44 +08:00

39 lines
1.1 KiB
Python

"""
Project Euler Problem 80: https://projecteuler.net/problem=80
Author: Sandeep Gupta
Problem statement: For the first one hundred natural numbers, find the total of
the digital sums of the first one hundred decimal digits for all the irrational
square roots.
Time: 5 October 2020, 18:30
"""
import decimal
def solution() -> int:
"""
To evaluate the sum, Used decimal python module to calculate the decimal
places up to 100, the most important thing would be take calculate
a few extra places for decimal otherwise there will be rounding
error.
>>> solution()
40886
"""
answer = 0
decimal_context = decimal.Context(prec=105)
for i in range(2, 100):
number = decimal.Decimal(i)
sqrt_number = number.sqrt(decimal_context)
if len(str(sqrt_number)) > 1:
answer += int(str(sqrt_number)[0])
sqrt_number_str = str(sqrt_number)[2:101]
answer += sum(int(x) for x in sqrt_number_str)
return answer
if __name__ == "__main__":
import doctest
doctest.testmod()
print(f"{solution() = }")