{"id":5265,"date":"2022-04-01T00:01:05","date_gmt":"2022-04-01T07:01:05","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5265"},"modified":"2022-04-09T08:05:00","modified_gmt":"2022-04-09T15:05:00","slug":"the-babylonian-square-root","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5265","title":{"rendered":"The Babylonian Square Root"},"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\" \/><\/p>\n<h2>Difficulty: &#9733; &#9733; &#9733; &#9734;<\/h2>\n<p>When I sought to write <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4250\">my own square root function<\/a> back in 2020, I based it on a series of steps from <a href=\"http:\/\/www.math.com\/school\/subject1\/lessons\/S1U1L9DP.html\" rel=\"noopener\" target=\"_blank\">the math.com website<\/a>. I figured, &#8220;Hey! These guys are math nerds. They know their stuff.&#8221; Turns out, the ancient Babylonians beat them to the punch &mdash; and have a better method for calculating a square root.<br \/>\n<!--more--><br \/>\nWhen they weren&#8217;t conquering or being conquered, the Babylonians devised a method for calculating square roots that involves averages and division. I&#8217;ve seen it explained a few times, but the math nerds tend to make the procedure sound more complex than it is. Here&#8217;s my take:<\/p>\n<ol>\n<li>Start with the positive integer to find its square root. For example, five.<\/li>\n<li>Obtain the average of any two values greater-than and less-than what the square root might be. Continuing with the example, the average of 4 and 2 is 3.<\/li>\n<li>Divide the original positive integer by the result of Step 2: 5\/3 = 1.666667.<\/li>\n<li>Repeat Steps 2 and 3 using the result of Step 3 as the less-than value in Step 2. So you continue with the average of 4 and 1.666667 = 2.833333, and then 5\/2.833333, and so on.<\/li>\n<\/ol>\n<p>Yes, even my version of the steps is confusing. So, Figure 1 attempts to illustrate the process, where Steps 1 and 2 (in the Figure) repeat a given number of times to obtain the square root of value <code>root<\/code>.<\/p>\n<div id=\"attachment_5268\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5268\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/04\/0401-figure1.png\" alt=\"Babylonian Method illustrated\" width=\"550\" height=\"292\" class=\"size-full wp-image-5268\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/04\/0401-figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/04\/0401-figure1-300x159.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2022\/04\/0401-figure1-500x265.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-5268\" class=\"wp-caption-text\">Figure 1. The Babylonian Method of finding the square root.<\/p><\/div>\n<p>In Figure 1, I removed the part about guessing the greater-than and less-than values. Instead, and in my exercise solution, I start the process by using the positive integer itself (<code>root<\/code>) and 1.0. Then I repeat the steps from there, which may take longer but it codes more efficiently.<\/p>\n<p>The more times you repeat Steps 1 and 2 (from Figure 1), the more precise the result. In my tests, seven times was adequate to obtain identical results as the C library&#8217;s <em>sqrt()<\/em> function, though some answers may suffer from the expected precision errors as floating-point calculations do.<\/p>\n<p>Your task for this month&#8217;s Exercise is to write the function <em>babylonian_sr()<\/em>, which uses the Babylonian Method to calculate a square root. The function need not be recursive; I didn&#8217;t use recursion in my solution.<\/p>\n<p>Here is a source code skeleton you can use for your solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_04_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_04_01-Lesson.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}\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>This code prompts the user for a positive integer. This value is sent to the <em>babylonian_sr()<\/em> function, which calculates the square root to seven digits of precision. The value is returned from the function as a <em>double<\/em>, which is output in the <em>main()<\/em> function.<\/p>\n<p>Here is a sample run of my solution:<\/p>\n<p><code>Enter a positive value: 5<br \/>\nThe square root of 5 is 2.236068<\/code><\/p>\n<p>Please try this Exercise on your own before you peek at <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5286\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>O, those clever Babylonians! Can you translate their square root technique into C code? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5265\">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":[3],"tags":[],"class_list":["post-5265","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5265","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=5265"}],"version-history":[{"count":12,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5265\/revisions"}],"predecessor-version":[{"id":5312,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5265\/revisions\/5312"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}