Actionscript:

var xn:Number = 1;

var xn1:Number = 0

var square:Number =39;


trace("find the sqrt of ", square);

trace("Math.sqrt: ", Math.sqrt(square))


// no starting approximation, just try up to 35 iterations

for(var i:int = 0; i<35; i++){

xn1 = .5 * (xn + square / xn);

if (xn1== xn){

trace("other sqrt: ", xn1);

break;

}

xn = xn1;

}

/*outputs

find the sqrt of 39

Math.sqrt: 6.244997998398398

other sqrt: 6.244997998398398

*/
I was reading about calculating square roots  not for speed optimization purposes, just to see some of the different ways to go about it. The above snippet uses the Babylonian method to find the square root of a number. I left out the first step of guessing at the square root... so this snippet is by no means efficient...
2 Comments
wow, couldnt believe this simple code.
cool right