Ronald Luis Benvenutti wrote: > To solve the problem of SQRT calculation, I'm sending the following > simpe routine I've been using for years. > .... > The routine does the following: > > It sums all odd numbers until the sum reaches the number 'n'. The > number of iterations (sums) is the integer sqrt of 'n'. > > Is there something so simple ? Ronald: As Steve Hardy has pointed out, the routine as written is quite slow. However, it can be improved (at the expense of a little extra code space) by performing a little pre-conditioning. The following is the QuickBASIC version of the square-root routine I posted here about a year and a half ago; it uses the same algorithm as your routine, with a few additional "if x = ...." lines at the start. -------------------------------------------------------------------- rem Andy Warren's "Two Adds and a Subtract" Square-Root Finder. rem 26 July 1994. rem rem For 16-bit radicands, this algorithm will make a maximum of 127 rem passes through the "mainloop" loop. On average, it'll make 60 rem passes. This seems like a lot, but each pass involves just one rem 16-bit subtract (with a test for negative result), an 8-bit rem increment, and an "add 2". rem rem For 16-bit radicands, "root" will always be in the range [0-255] rem and "s" will always be in the range [1-511]. start: input "Radicand (0-65535): ";x rem The following 6 "if" statements are included only for speed of rem execution (they approximately double the average computation rem speed). They can be removed if program size is more important. if x > 16383 then x = x - 16384: root = 128: s = 257: goto main if x > 4095 then x = x - 4096: root = 64: s = 129: goto main if x > 1023 then x = x - 1024: root = 32: s = 65: goto main if x > 255 then x = x - 256: root = 16: s = 33: goto main if x > 63 then x = x - 64: root = 8: s = 17: goto main if x > 15 then x = x - 16: root = 4: s = 9: goto main root = 0: s = 1 main: x = x - s: if x < 0 then goto done root = root + 1: s = s + 2 goto main done: print "Square root = "; root: print goto start ------------------------------------------------------------------- -Andy Andrew Warren - fastfwd@ix.netcom.com Fast Forward Engineering, Vista, California http://www.geocities.com/SiliconValley/2499