{"id":4456,"date":"2020-11-21T00:01:21","date_gmt":"2020-11-21T08:01:21","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4456"},"modified":"2020-11-23T06:15:57","modified_gmt":"2020-11-23T14:15:57","slug":"the-golden-ration-recursion-version","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4456","title":{"rendered":"The Golden Ratio &#8211; Recursion Version"},"content":{"rendered":"<p>Oh, how I distrusted recursion when I was a budding programmer. It&#8217;s just a tough concept to wrap your head around, especially if you&#8217;re an old warhorse Assembly programmer like me who lives in fear of blowing up the stack. Trivial asides aside, recursion often presents an elegant and efficient way to solve a programming puzzle.<br \/>\n<!--more--><br \/>\nThis time around, the puzzle is to calculate the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Golden_ratio\" rel=\"noopener noreferrer\" target=\"_blank\">golden ratio<\/a>, &phi;, which was covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4444\">last week&#8217;s Lesson<\/a>. Figure 1 shows the nifty animation I created to illustrate the ratio, which is wordy and strange to explain, so enjoy the pretty colors.<\/p>\n<div id=\"attachment_4449\" style=\"width: 363px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4449\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/11\/1114_figure1_goldenratio.gif\" alt=\"Golden ratio animation showing a+b\/a\" width=\"353\" height=\"369\" class=\"size-full wp-image-4449\" \/><p id=\"caption-attachment-4449\" class=\"wp-caption-text\">Figure 1. An animation showing the relationship between &#8216;a&#8217; and &#8216;b&#8217; relative to each other.<\/p><\/div>\n<p>Last week&#8217;s Lesson covered the straightforward way of calculation the value of &phi;: (1+&#8730;5)\/2 = 1.6180339887&#8230;<\/p>\n<p>Other methods exist to calculate the golden ratio, specifically continued fractions. One such fraction is illustrated in Figure 2. Please! Don&#8217;t let its appearance alarm you.<\/p>\n<div id=\"attachment_4453\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4453\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/11\/1114-figure1_phi-continued-fraction.png\" alt=\"phi expressed as a continued fraction, which really looks scary\" width=\"500\" height=\"240\" class=\"size-full wp-image-4453\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/11\/1114-figure1_phi-continued-fraction.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/11\/1114-figure1_phi-continued-fraction-300x144.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-4453\" class=\"wp-caption-text\">Figure 1. Calculating &phi; as a continued fraction &#8211; something that begs for a recursive function.<\/p><\/div>\n<p>These continued fraction boogers can be elegantly transformed into a <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1060\">recursive<\/a> function. Such an expression may not be obvious at first, but because I&#8217;m a nerd and I&#8217;ve been doing the programming thing a while, it took only a moment to figure it out:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_11_21-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_11_21-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\ndouble phi(double p, int precision)\r\n{\r\n    while(precision)\r\n        return( p + 1\/phi(p, precision-1) );\r\n    return(p);\r\n}\r\n\r\nint main()\r\n{\r\n    double gr;\r\n\r\n    gr = phi(1.0,15);\r\n    printf(\"The golden radio is %f\\n\",gr);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>phi()<\/em> function is recursive. Its two arguments are <em>double<\/em> <code>p<\/code>, and <em>int<\/em> <code>precision<\/code>. The <code>precision<\/code> argument determines how many times the function recurses, which also plays into the accuracy of the value returned.<\/p>\n<p>Within the <em>return<\/em> statement, the expression <code>p + 1\/phi(p, precision-1)<\/code> is a representation of the continued fraction shown in Figure 2. This result isn&#8217;t calculated until the value of <code>precision<\/code> reaches zero, and the <em>while<\/em> loop stops. At this point, the final <em>return<\/em> statement is executed and the function unwinds, modifying the value of <code>p<\/code> until the golden ratio is calculated. Here&#8217;s a sample run:<\/p>\n<p><code>The golden radio is 1.618034<\/code><\/p>\n<p>I experimented a bit with the precision value passed at Line 15. The value 15 seems to be the minimum required to render a proper value for &phi; for the digits output.<\/p>\n<p>If I were required to use &phi; in my code, I would most likely declare it as a defined constant, stretching it out to 20 or so digits as necessary:<\/p>\n<p><code>#define PHI 1.6180339887498948482<\/code><\/p>\n<p>Still, programming is often about exercising the mind as often as it&#8217;s about solving math problems. Though recursion can be a frustrating concept, I enjoy working out a recursive function. It can be delightful when it actually works and doesn&#8217;t blow up the computer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a C coder, I am delighted to offer any code that takes advantage of recursion. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4456\">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":[2],"tags":[],"class_list":["post-4456","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4456","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=4456"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4456\/revisions"}],"predecessor-version":[{"id":4486,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4456\/revisions\/4486"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}