{"id":4499,"date":"2020-12-19T00:01:05","date_gmt":"2020-12-19T08:01:05","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4499"},"modified":"2020-12-19T08:53:24","modified_gmt":"2020-12-19T16:53:24","slug":"harmonic-series-divergence-and-c-code","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4499","title":{"rendered":"Harmonic Series, Divergence, and C Code"},"content":{"rendered":"<p>A harmonic series is a mathematical contraption that deals with cascading fractions. Like the Fibonacci series, I thought I could easily code a harmonic series in C &mdash; which I did, but not before reading up on the topic of divergence.<br \/>\n<!--more--><br \/>\nPicking up from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4495\">last week&#8217;s Lesson<\/a>, a harmonic series is shown in Figure 1, where it represents the total of a series of smaller and smaller fractions. What is the total?<\/p>\n<div id=\"attachment_4496\" style=\"width: 654px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4496\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/11\/1212-figure1.png\" alt=\"A harmonic series illustrated with weirdo math symbols\" width=\"644\" height=\"138\" class=\"size-full wp-image-4496\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/11\/1212-figure1.png 644w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/11\/1212-figure1-300x64.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/11\/1212-figure1-500x107.png 500w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><p id=\"caption-attachment-4496\" class=\"wp-caption-text\">Figure 1. A harmonic series illustrated in a most frightening manner.<\/p><\/div>\n<p>Never mind doing the math! What I did was to write computer code that made the calculations for me, adding the values 1\/1, 1\/2, on up through 1\/999. Here is the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_12_19-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2020_12_19-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n    float t;\r\n\r\n    t = 1.0;\r\n    for( x=2; x&lt;=1000; x++ )\r\n    {\r\n        t += 1.0 \/ (float)x;\r\n    }\r\n    printf(\"The sum of the harmonic series is %.4f\\n\",t);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop at Line 9 does a sum of the first 1,000 items in the harmonic series. Each spin of the loop adds another fraction to the total, from 1\/1 to 1\/999. After all these spins, here is the output:<\/p>\n<p><code>The sum of the harmonic series is 7.4855<\/code><\/p>\n<p>I felt rather good about myself for performing this calculation, though I then proceeded to increase the looping value from 1,000 to 1,000,000. Here is the new output:<\/p>\n<p><code>The sum of the harmonic series is 14.3574<\/code><\/p>\n<p>Whoa! It&#8217;s getting bigger.<\/p>\n<p>Before I increased the looping value further, I wondered what the sum actually was. So I watched the rest of the <a href=\"https:\/\/youtu.be\/vQE6-PLcGwU\" rel=\"noopener noreferrer\" target=\"_blank\">video on harmonic series<\/a> only to discover that the series is divergent. Which means that its sum is infinity.<\/p>\n<p>The divergence concept was surprising to me. After studying Zeno&#8217;s paradoxes, specifically Achilles and the tortoise, I would think that a harmonic series would flatten out at a certain point. But no! Many proofs abound to show that the series is divergent and continues to grow to infinity.<\/p>\n<p>In fact, in the video, it states that to reach the value of 100.0, my code must loop 15.092&#215;10<sup>42<\/sup> times. It would take a computer days to perform this calculation. And somehow it eventually gets to infinity.<\/p>\n<p>Not every series climbs to infinity. In fact, the sum of a series of powers-of-two fractions flattens out to the value 2.0. This series is illustrated in Figure 2.<\/p>\n<div id=\"attachment_4508\" style=\"width: 451px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4508\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/12\/1219-figure2.png\" alt=\"An even number series, showing fractions 1\/2, 1\/4, 1\/8 on and on.\" width=\"441\" height=\"134\" class=\"size-full wp-image-4508\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/12\/1219-figure2.png 441w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/12\/1219-figure2-300x91.png 300w\" sizes=\"auto, (max-width: 441px) 100vw, 441px\" \/><p id=\"caption-attachment-4508\" class=\"wp-caption-text\">Figure 1. An power-of-two series thing.<\/p><\/div>\n<p>My code to generate a power-of-two series hinges upon a <em>for<\/em> loop:<\/p>\n<p><code>for( x=2; x&lt;=1000; x*=2 )<\/code><\/p>\n<p>This loop generates <code>x<\/code> values as multiples of 2: 2, 4, 8, 16, and so on. An expression within the loop calculates the sum of fractions 1+1\/2+1\/4&#8230; as illustrated in the Figure.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_12_19-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2020_12_19-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n    float t;\r\n\r\n    t = 1.0;\r\n    for( x=2; x&lt;=1000; x*=2 )\r\n    {\r\n        t += 1.0 \/ (float)x;\r\n        printf(\"+1\/%d = %.4f\\n\",x,t);\r\n    }\r\n    printf(\"The sum of the even series is %.4f\\n\",t);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 11 calculates the sum of the powers-of-two series, updating the value in variable <code>t<\/code> as it goes. Because the values increase rapidly, the code outputs the equations and running total at Line 12. The final total is output at Line 14:<\/p>\n<p><code>+1\/2 = 1.5000<br \/>\n+1\/4 = 1.7500<br \/>\n+1\/8 = 1.8750<br \/>\n+1\/16 = 1.9375<br \/>\n+1\/32 = 1.9688<br \/>\n+1\/64 = 1.9844<br \/>\n+1\/128 = 1.9922<br \/>\n+1\/256 = 1.9961<br \/>\n+1\/512 = 1.9980<br \/>\nThe sum of the even series is 1.9980<\/code><\/p>\n<p>Unlike the harmonic series, which grows to infinity, this series approaches 2.0. It is non-divergent, and code easily proves so. None of this would otherwise be interesting to me, but because I can code it in C, it becomes a fun little challenge.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My inner math nerd comes out again as I explore the concepts of harmonic series, which work better for me in C code than in maths class. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4499\">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-4499","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\/4499","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=4499"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4499\/revisions"}],"predecessor-version":[{"id":4533,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4499\/revisions\/4533"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}