{"id":5286,"date":"2022-04-08T00:01:13","date_gmt":"2022-04-08T07:01:13","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5286"},"modified":"2022-04-02T11:44:57","modified_gmt":"2022-04-02T18:44:57","slug":"the-babylonian-square-root-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5286","title":{"rendered":"The Babylonian Square Root &#8211; Solution"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/04\/04_hammurabi-can-code-that.png\" alt=\"Hammurabi\" width=\"350\" height=\"215\" class=\"alignnone size-full wp-image-5287\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/04\/04_hammurabi-can-code-that.png 350w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/04\/04_hammurabi-can-code-that-300x184.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><br \/>\nI was blown away by the elegance and simplicity of the Babylonian Method to calculate a square root, which is the topic of <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5265\">this month&#8217;s Exercise<\/a>. I only hope that my solution lives up the challenge, lest I suffer the fate of the Assyrians.<br \/>\n<!--more--><br \/>\nThe challenge is to write the <em>babylonian_sr()<\/em> function, which consumes a <em>double<\/em> value and returns a <em>double<\/em> value, a square root. I set a precision value of seven and use the passed argument as the &#8220;high&#8221; value, 1.0 as the initial &#8220;low&#8221; value; refer to the original post if you&#8217;re confused by these labels. Here is my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_04-Exercise.c\" rel=\"noopener\" target=\"_blank\">2022_04-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\ndouble babylonian_sr(double root)\r\n{\r\n    double low,high;\r\n    int x;\r\n    const int precision = 7;\r\n\r\n    low = 1.0;\r\n    high = root;\r\n    for( x=0; x&lt;precision; x++ )\r\n    {\r\n        high = (high+low)\/2.0;\r\n        low = root\/high;\r\n    }\r\n    return(low);\r\n}\r\n\r\nint main()\r\n{\r\n    double pv,sr;\r\n\r\n    printf(\"Enter a positive value: \");\r\n    scanf(\"%lf\",&amp;pv);\r\n    if( pv &lt;= 0 )\r\n        return(1);\r\n    sr = babylonian_sr(pv);\r\n    printf(\"The square root of %.0f is %f\\n\",\r\n            pv,\r\n            sr\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>My <em>babylonian_sr()<\/em> function uses variable names stolen from Figure 1 in the Exercise post: <code>root<\/code> is the value passed; <code>low<\/code> and <code>high<\/code> are the less-than and greater-than values used in the average calculation.<\/p>\n<p>The <code>low<\/code> value is set to 1.0 at Line 9.<\/p>\n<p>The <code>high<\/code> value is set equal to <code>root<\/code> at Line 10. Remember, <code>root<\/code> doesn&#8217;t change throughout the function. (It could be declared <em>const<\/em>, but that&#8217;s just more typing.)<\/p>\n<p>Variable <code>x<\/code> in the <em>for<\/em> loop sets the precision, the number of times the two steps repeat. I chose the precision value 7, set at Line 7.<\/p>\n<p>Step 1 takes place at Line 13. The new <code>high<\/code> value is determined as the average of <code>high<\/code> and <code>low<\/code>.<\/p>\n<p>Step 2 takes place at Line 14. The new <code>low<\/code> value is calculated as the original number, <code>root<\/code>, divided by <code>high<\/code>.<\/p>\n<p>The process repeats <code>precision<\/code> times, and the result stored in <code>low<\/code> is returned at Line 16.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Enter a positive value: 5<br \/>\nThe square root of 5 is 2.236068<\/code><\/p>\n<p>For some large values, the result may not be perfect. If you find such an issue, increase the value of variable <code>precision<\/code> to something greater than seven, but keep in mind that floating point values aren&#8217;t precise.<\/p>\n<p>If you really want to test your <em>babylonian_sr()<\/em> function solution, add statements in the <em>main()<\/em> function that compare your function&#8217;s output with C&#8217;s <em>sqrt()<\/em> function. Remember to include the <code>math.h<\/code> header, and if you use Linux, you must link in the <code>m<\/code> library to build the code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was blown away by the elegance and simplicity of the Babylonian Method to calculate a square root, which is the topic of this month&#8217;s Exercise. I only hope that my solution lives up the challenge, lest I suffer the &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5286\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-5286","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5286"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5286\/revisions"}],"predecessor-version":[{"id":5303,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5286\/revisions\/5303"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}