Skip to main content

newton_sqrt.c (Source)

#define X_INIT (1 << 8) /* Arbitrary initial value for x */
unsigned long newton_sqrt(unsigned long z)
{
unsigned long x;
unsigned long x_next = X_INIT;
int maxiter = 8 * sizeof(z);
while (x != x_next  && maxiter-- > 0) {
x = x_next;
x_next = (x + z / x) / 2;
}
return x;
}