{"id":248,"date":"2009-04-10T10:40:35","date_gmt":"2009-04-10T17:40:35","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1145"},"modified":"2009-04-10T10:40:35","modified_gmt":"2009-04-10T17:40:35","slug":"babylonian-method-for-square-root","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=248","title":{"rendered":"Babylonian Method for Square Root"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar xn:Number = 1;\r\nvar xn1:Number = 0\r\nvar square:Number =39;\r\n\r\ntrace(&quot;find the sqrt of &quot;, square);\r\ntrace(&quot;Math.sqrt: &quot;, Math.sqrt(square))\r\n\r\n\/\/ no starting approximation, just try up to 35 iterations\r\nfor(var i:int = 0; i&lt;35; i++){\r\n\txn1 = .5 * (xn  + square \/ xn);\r\n\tif (xn1== xn){\r\n\t\ttrace(&quot;other sqrt: &quot;, xn1);\r\n\t\tbreak;\r\n\t}\r\n\txn = xn1;\r\n}\r\n\/*outputs\r\nfind the sqrt of  39\r\nMath.sqrt:  6.244997998398398\r\nother sqrt:  6.244997998398398\r\n*\/\r\n<\/pre>\n<p>I was reading about calculating square roots &#8211; not for speed optimization purposes, just to see some of the different ways to go about it. The above snippet uses the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Methods_of_computing_square_roots#Babylonian_method\">Babylonian method<\/a> to find the square root of a number. I left out the first step of guessing at the square root&#8230; so this snippet is by no means efficient&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var xn:Number = 1; var xn1:Number = 0 var square:Number =39; trace(&quot;find the sqrt of &quot;, square); trace(&quot;Math.sqrt: &quot;, Math.sqrt(square)) \/\/ no starting approximation, just try up to 35 iterations for(var i:int = 0; i&lt;35; i++){ xn1 = .5 * (xn + square \/ xn); if (xn1== xn){ trace(&quot;other sqrt: &quot;, xn1); break; } xn [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[3,4],"class_list":["post-248","post","type-post","status-publish","format-standard","hentry","category-math","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=248"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}