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
()
| 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__": |