{"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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/*Returns the square root of n. Note that the function *\/<br\/>float squareRoot(float n)<br\/>{<br\/>  \/*We are using n itself as initial approximation<br\/>   This can definitely be improved *\/<br\/>  float x = n;<br\/>  float y = 1;<br\/>  float e = 0.000001; \/* e decides the accuracy level*\/<br\/>  while(x - y &gt; e)<br\/>  {<br\/>    x = (x + y)\/2;<br\/>    y = n\/x;<br\/>  }<br\/>  return x;<br\/>}<br\/> <br\/>\/* Driver program to test above function*\/<br\/>int main()<br\/>{<br\/>  int n = 50;<br\/>  printf (&quot;Square root of %d is %f&quot;, n, squareRoot(n));<br\/>  getchar();<br\/>}<\/code><\/pre> <\/div>\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) &gt; 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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C program<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/*Returns the square root of n. Note that the function<br\/>  will not work for numbers which are not perfect squares*\/<br\/>unsigned int squareRoot(int n)<br\/>{<br\/>  int x = n;<br\/>  int y = 1;<br\/>  while(x &gt; y)<br\/>  {<br\/>    x = (x + y)\/2;<br\/>    y = n\/x;<br\/>  }<br\/>  return x;<br\/>}<br\/> <br\/>\/* Driver program to test above function*\/<br\/>int main()<br\/>{<br\/>  int n = 49;<br\/>  printf (&quot; root of %d is %d&quot;, n, squareRoot(n));<br\/>  getchar();<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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}]}}