home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / arch / 10777 < prev    next >
Encoding:
Text File  |  1992-11-15  |  1.8 KB  |  56 lines

  1. Newsgroups: comp.arch
  2. Path: sparky!uunet!mcsun!sunic!dkuug!imada!news
  3. From: breese@monet.imada.ou.dk (Bjoern Reese)
  4. Subject: Re: integer distance or sqrt
  5. Message-ID: <1992Nov16.110102.17077@imada.ou.dk>
  6. Sender: news@imada.ou.dk (USENET News System)
  7. Organization: Dept. of Math. & Computer Science, Odense University, Denmark
  8. References: <CLIFFC.92Nov15184340@miranda.rice.edu>
  9. Date: Mon, 16 Nov 1992 11:01:02 GMT
  10. Lines: 44
  11.  
  12. In article <CLIFFC.92Nov15184340@miranda.rice.edu> cliffc@rice.edu (Cliff  
  13. Click) writes:
  14. > bbs.dennis@tsoft.sf-bay.org (Dennis Yelle) writes:
  15. > > I first considered asking for a integer sqrt, but maybe if the context is 
  16. > I sent him an integer square root code using Newton's Method.  I've done 
  17. > this problem before and I'm sure there are better methods out there.  Does 
  18. > somebody want to post/email something better than Newton's?
  19.  
  20. Try this one:
  21.  
  22. Heron(x)   {Fast integer squareroot routine}
  23.    IF x <= 64 THEN
  24.       y = 2 + (x >> 3)  {shift right 3 times}
  25.    ELSE
  26.       IF x <= 1024 THEN
  27.          y = 8 + (x >> 5)
  28.       ELSE
  29.          IF x <= 8192 THEN
  30.             y = 32 + (x >> 7)
  31.          ELSE
  32.             IF x <= 32767 THEN
  33.                y = 64 + (x >> 8)
  34.             ELSE
  35.                y = 128 + (x >> 9)
  36.             ENDIF
  37.          ENDIF
  38.       ENDIF
  39.    ENDIF
  40.    root := (y + x DIV y) >> 1
  41.  
  42. This algorithm is faster than Newton's Method, as it only
  43. consists of compares, adds and shifts, and no loops :) Oh
  44. yes, and a single integer division.
  45.  
  46. I found it some years ago in a german magazine called "MC."
  47. It was dubbed "Heron's Algorithm." As you can see it is a
  48. 16 bit implementation. Does anybody have further information
  49. about this algorithm? (like, how to extend it to 32 bits.)
  50.  
  51. --
  52.  
  53. Bjoern Reese                   |     Email: breese@imada.ou.dk
  54. Odense University, Denmark     |     Voice: +45 65 932 182 (private)
  55.