[issue36957] Speed up math.isqrt

Serhiy Storchaka report at bugs.python.org
Sun May 19 03:16:22 EDT 2019


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

I have also some ideas about algorithmic optimizations (they need to be tested). In classic formula $a_{i+1} = a_i + (n - a_i^2)/(2*a_i)$ we can calculate $n - a_i^2$ as $(n - a_{i-1}^2) - (a_i^2 - a_{i-1})^2 = (n - a_{i-1}^2) - (a_i^2 - a_{i-1})*(a_i^2 + a_{i-1})$. $n - a_i^2$ usually is much smaller than $n$, so this can speed up subtraction and division. Things become more complicated when use shifts as in your formula, but I think that we can get benefit even in this case. This can also speed up the final check $a_i^2 <= n$.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36957>
_______________________________________


More information about the Python-bugs-list mailing list