Square Root Function

I'm working on a project that needs a square root function.

Here's what I've come up with, for others to use.

This method dating back to the Babylonians (1700 B.C.) is known as the Babylonian Algorithm.

If A > 0 is a guess of the square root of the positive number Q, then a better approximation to SQRT(Q) is given by
B = (A + Q/A)/2.
 


;;###############################
; square root function
;
; 
This method dating back to the Babylonians (1700 B.C.) is known as the Babylonian Algorithm.
; If 
A > 0 is a guess of the square root of the positive number Q, 
; 
then a better approximation to ?Q is given by B = (A + Q/A)/2.

Declare Function    sqroot(target As Word) Returns Word

Declare Word    guess            ; the [improving] estimate of the sqrot
Declare Word    lastguess        ; what we guessed last time around
Declare Word    closeenough = 1    ; how close do we need to get
Declare Word    change        ; when checking guess against lastguess
Declare Word    tempw

guess 
= target / 2            ; guess half way
lastguess 
= 0
change 
= guess - lastguess

While change > closeenough        ; check how close this guess is to last one

    tempw 
= target / guess        ; newguess = (Target/Guess + Guess) / 2
    guess 
= guess + tempw
    guess 
= guess / 2
    
If guess > lastguess Then    
        change 
= guess - lastguess
    
Else
        
change = lastguess - guess
    End 
If
    
lastguess = guess            ; remember for next time around

End 
While

Return 
guess
End 
Function

;
###############################

Begin Program

Declare Word    answer
Declare Byte     answerb

answer 
= sqroot(2906)
answerb = LowByte(answer)    ; so we can display it
DisplayNumber answerb
Wait 500


End Program