MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_080/sol1.py:13–32  ·  view source on GitHub ↗

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

()

Source from the content-addressed store, hash-verified

11
12
13def 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
35if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected