Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!leah!rpi!batcomputer!cornell!uw-beaver!uw-entropy!dataio!bright From: bright@Data-IO.COM (Walter Bright) Newsgroups: comp.lang.c Subject: Re: Integer square root routine needed. Message-ID: <2082@dataio.Data-IO.COM> Date: 2 Aug 89 17:35:24 GMT References: <7415@ecsvax.UUCP> Reply-To: bright@dataio.Data-IO.COM (Walter Bright) Organization: Data I/O Corporation; Redmond, WA Lines: 24 In article <7415@ecsvax.UUCP> utoddl@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.