Challenge: optimizing isqrt

Christian Gollwitzer auriocus at gmx.de
Sat Nov 1 04:25:40 EDT 2014


Am 01.11.14 09:13, schrieb Chris Angelico:
> On Sat, Nov 1, 2014 at 7:02 PM, Christian Gollwitzer <auriocus at gmx.de> wrote:
>> Your above algorithm is obviously doing Heron- or Newton-Raphson iterations,
>> so the same as with floating point math. The first line before the while
>> loop computes some approximation to sqrt(n). Instead of doing bit shuffling,
>> you could compute this by FP math and get closer to the desired result,
>> unless the integer is too large to be represented by FP. Now, the
>> terminating condition seems to rely on the fact that the initial estimate
>> x>=sqrt(n), but I don't really understand it. My guess is that if you do
>> x=int(sqrt(n)), then do the first iteration, then swap x and y such that
>> x>y, then enter the loop, you would simply start with a better estimate in
>> case that the significant bits can be represented by the float.
>
> Part of the point of that algorithm is that it never uses FP, and is
> therefore not limited by FP restrictions.

which are???





More information about the Python-list mailing list