I've been doing some research looking for a relatively fast square root algorithm that operates on big integers. I found a couple of routines on here. The first one (below) was written in C...
int isqrt(int n)
{
int b = 0;
while(n >= 0)
{
n = n - b;
b = b + 1;
n = n - b;
}
return b - 1;
}
...which I found here: Looking for an efficient integer square root algorithm for ARM Thumb2
I implemented this routine due to its simplicity and efficient use of buffer space. However, as it is simple, the performance is beyond unacceptable. It does work and gives the correct answer when returning just b
and not b - 1
.
So in my quest of a better algorithm, I came across the following algorithm written in Java...
public static BigInteger sqrt(BigInteger x) {
BigInteger div = BigInteger.ZERO.setBit(x.bitLength()/2);
BigInteger div2 = div;
// Loop until we hit the same value twice in a row, or wind
// up alternating.
for(;;) {
BigInteger y = div.add(x.divide(div)).shiftRight(1);
if (y.equals(div) || y.equals(div2))
return y;
div2 = div;
div = y;
}
}
...which I found on this question: How can I find the Square Root of a Java BigInteger?
I will gladly admit that I am not well versed in Java, so the question that I have is what does the line BigInteger y = div.add(x.divide(div)).shiftRight(1);
do?
In my limited knowledge, I have a possible conversion of the loop to the C code fragment below, but I don't know enough about Java to be sure.
while (1)
{
x /= div;
div += x;
y = x >> 1;
if (y == div || y == div2) return(y);
div2 = div;
div = y;
}
Is this correct?