Integer square root routine needed.

Walter Bright bright at Data-IO.COM
Thu Aug 3 03:35:24 AEST 1989


In article <7415 at ecsvax.UUCP> utoddl at ecsvax.UUCP (Todd M. Lewis) writes:
>This may be the wrong place to post this, but I need C source
>to a reasonably fast integer square root routine.

/***************************
 * Compute the square root using Newton's method.
 * Donated to Public Domain.
 */

unsigned long ulsqrt(l)
unsigned long l;
{	unsigned long c1;

	/* Perhaps we could do better than this for a first guess	*/
	c1 = (l >> 1) + 1;
	while (c1 * c1 > l)
	{
		c1 = (c1 + l / c1) >> 1;
		/*printf("c1 = %ld\n",c1);*/
	}
	return c1;
}

If you can improve on this, please let me know.



More information about the Comp.lang.c mailing list