Square Root Digital Expansion¶

Unfortunately, just calling Python's sqrt won't cut it - floats don't have nearly enough digits of precision.

The easiest approach here is to use SageMath's arbitrary precision routines, or Python's decimal package. We need to watch for rounding issues, so we'll calculate slightly more than 100 digits and truncate.

In [1]:
def digital_sum(n):
    s = sqrt(n).n(digits=110)
    digits = (int(d) for d in str(s)[:101] if d != '.')
    return sum(digits)
    

sum(digital_sum(n) for n in range(1, 101) if not is_square(n))
Out[1]:
40886

If you'd rather not use those tools for some reason, another simple approach is to take advantage of Python's arbitrary precision integers and calculate the integer square root of $n \times 10^k$ for a sufficiently large $k$ ($k \geq 200$ for this problem).

In [2]:
def digital_sum(n):
    s = isqrt(n * 10^200)
    digits = (int(d) for d in str(s)[:100])
    return sum(digits)
    

sum(digital_sum(n) for n in range(1, 101) if not is_square(n))
Out[2]:
40886

If you want to go the extra mile, there are several algorithms for computing square roots that you can implement yourself, such as Heron's method, which is a special case of Newton's method for solving $x^2 - n = 0$. The method works by starting with an initial estimate $x_0$ (such as $\frac{n}{2}$), then repeatedly calculating $$x_{k+1} = \frac{1}{2}\left(x_k + \frac{n}{x_k}\right)$$ until $|x_{k+1} - x_k|$ is sufficiently small. For computing the integer square root, this can be when $|x_{k+1} - x_k| < 1$.

Copyright (C) 2025 filifa¶

This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license and the BSD Zero Clause license.