{"id":25630,"date":"2017-10-15T20:03:30","date_gmt":"2017-10-15T14:33:30","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25630"},"modified":"2017-10-15T20:03:30","modified_gmt":"2017-10-15T14:33:30","slug":"c-programming-babylonian-method-square-root","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-babylonian-method-square-root\/","title":{"rendered":"C Programming-Babylonian method for square root"},"content":{"rendered":"<p><strong>1.Algorithm:<\/strong><\/p>\n<p>This method can be derived from (but predates) Newton \u2013 Raphson method.<br \/>\n1 Start with an arbitrary positive start value x (the closer to the<br \/>\nroot, the better).<br \/>\n2 Initialize y = 1.<br \/>\n3. Do following until desired approximation is achieved.<br \/>\na) Get the next approximation for root using average of x and y<br \/>\nb) Set y = n\/x<br \/>\n<strong>2.Implementation:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F*Returns%20the%20square%20root%20of%20n.%20Note%20that%20the%20function%20*%2F%0Afloat%20squareRoot(float%20n)%0A%7B%0A%20%20%2F*We%20are%20using%20n%20itself%20as%20initial%20approximation%0A%20%20%20This%20can%20definitely%20be%20improved%20*%2F%0A%20%20float%20x%20%3D%20n%3B%0A%20%20float%20y%20%3D%201%3B%0A%20%20float%20e%20%3D%200.000001%3B%20%2F*%20e%20decides%20the%20accuracy%20level*%2F%0A%20%20while(x%20-%20y%20%3E%20e)%0A%20%20%7B%0A%20%20%20%20x%20%3D%20(x%20%2B%20y)%2F2%3B%0A%20%20%20%20y%20%3D%20n%2Fx%3B%0A%20%20%7D%0A%20%20return%20x%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20int%20n%20%3D%2050%3B%0A%20%20printf%20(%22Square%20root%20of%20%25d%20is%20%25f%22%2C%20n%2C%20squareRoot(n))%3B%0A%20%20getchar()%3B%0A%7D\u201d message=\u201dC\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Example:<\/strong><\/p>\n<pre>n = 4 \/*n itself is used for initial approximation*\/\r\nInitialize x = 4, y = 1\r\nNext Approximation x = (x + y)\/2 (= 2.500000), \r\ny = n\/x  (=1.600000)\r\nNext Approximation x = 2.050000,\r\ny = 1.951220\r\nNext Approximation x = 2.000610,\r\ny = 1.999390\r\nNext Approximation x = 2.000000, \r\ny = 2.000000\r\nTerminate as (x - y) > e now.\r\n<\/pre>\n<p>If we are sure that n is a perfect square, then we can use following method. The method can go in infinite loop for non-perfect-square numbers. For example, for 3 the below while loop will never terminate.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F*Returns%20the%20square%20root%20of%20n.%20Note%20that%20the%20function%0A%20%20will%20not%20work%20for%20numbers%20which%20are%20not%20perfect%20squares*%2F%0Aunsigned%20int%20squareRoot(int%20n)%0A%7B%0A%20%20int%20x%20%3D%20n%3B%0A%20%20int%20y%20%3D%201%3B%0A%20%20while(x%20%3E%20y)%0A%20%20%7B%0A%20%20%20%20x%20%3D%20(x%20%2B%20y)%2F2%3B%0A%20%20%20%20y%20%3D%20n%2Fx%3B%0A%20%20%7D%0A%20%20return%20x%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20int%20n%20%3D%2049%3B%0A%20%20printf%20(%22%20root%20of%20%25d%20is%20%25d%22%2C%20n%2C%20squareRoot(n))%3B%0A%20%20getchar()%3B%0A%7D\u201d message=\u201dC program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>C Programming &#8211; Babylonian method for square root &#8211; Mathematical Algorithms &#8211; If we are sure that n is a perfect square, then we can use following method.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,1,74058],"tags":[74390,74409,74413,74407,74404,74417,74418,74385,74415,74408,74419,74422,74410,74405,74388,74402,74395,74401,74406,74382,74416,74414,74403,74392,74423,74398,74424,74384,74400,74427,74397,74387,74381,57524,74425,74383,74420,74426,74412],"class_list":["post-25630","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-coding","category-mathematical-algorithms","tag-calculation-of-square-root","tag-calculator-of-square-root","tag-calculator-root","tag-calculator-square-root","tag-calculator-to-find-square-root","tag-calculator-with-square-root","tag-decimal-to-square-root","tag-easy-way-to-find-square-of-a-number","tag-find-the-square-root-by-division-method","tag-finding-square-root-manually","tag-finding-the-square-root-of-a-number","tag-formula-for-finding-square-root-of-a-number","tag-formula-for-square-root","tag-how-can-we-find-square-root-of-a-number","tag-how-to-calculate-square-root","tag-how-to-calculate-under-root","tag-how-to-do-square-roots","tag-how-to-find-out-square-root","tag-how-to-find-square-root","tag-how-to-find-square-root-easily","tag-how-to-find-square-root-of-2","tag-how-to-find-square-root-of-5","tag-how-to-find-square-root-of-a-number-quickly","tag-how-to-get-square-root","tag-how-to-solve-square-roots","tag-how-to-take-square-root","tag-how-we-find-square-root","tag-long-division-method-for-square-root","tag-math-calculator-square-root","tag-root-calculator","tag-solving-square-roots","tag-square-root-by-division-method","tag-square-root-by-long-division-method","tag-square-root-calculator","tag-square-root-formula","tag-square-root-function-in-java","tag-square-root-in-math","tag-to-find-square-root-of-a-number","tag-under-root-calculator"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25630"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25630\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}