# Babylonian Method for Square Root

Actionscript:
1. var xn:Number = 1;
2. var xn1:Number = 0
3. var square:Number =39;
4.
5. trace("find the sqrt of ", square);
6. trace("Math.sqrt: ", Math.sqrt(square))
7.
8. // no starting approximation, just try up to 35 iterations
9. for(var i:int = 0; i<35; i++){
10.     xn1 = .5 * (xn  + square / xn);
11.     if (xn1== xn){
12.         trace("other sqrt: ", xn1);
13.         break;
14.     }
15.     xn = xn1;
16. }
17. /*outputs
18. find the sqrt of  39
19. Math.sqrt:  6.244997998398398
20. other sqrt:  6.244997998398398
21. */

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...

1. easy math for kids
Posted July 6, 2011 at 4:44 am | Permalink

wow, couldnt believe this simple code.

2. Zevan
Posted August 29, 2011 at 8:21 am | Permalink

cool right 