forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol.py
52 lines (41 loc) · 1.25 KB
/
sol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import decimal
from math import floor
"""
Square root digital expansion
It is well known that if the square root of a natural number is not an integer,
then it is irrational. The decimal expansion of such square roots is infinite
without any repeating pattern at all.
The square root of two is 1.41421356237309504880..., and the digital sum of the
first one hundred decimal digits is 475.
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.
>>> solution()
40886
"""
def solution() -> int:
"""Return the total of the digital sums of the first one hundred decimal digits for
all the irrational square roots.
>>> solution()
40886
"""
n = 100
p = 100
tot = 0
for i in range(1, n + 1):
sm = 0
decimal.getcontext().prec = p + 10
fv = decimal.Decimal(i).sqrt()
if fv - floor(fv) == 0:
continue
else:
fv = str(fv).replace(".", "")
fv = fv[0:p]
fv = int(fv)
while fv != 0:
sm = sm + (fv % 10)
fv = fv // 10
tot += sm
return tot
if __name__ == "__main__":
res = solution()
print(res)