{"id":4166,"date":"2020-06-08T00:01:24","date_gmt":"2020-06-08T07:01:24","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4166"},"modified":"2020-06-06T11:23:57","modified_gmt":"2020-06-06T18:23:57","slug":"getting-to-eulers-number-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4166","title":{"rendered":"Getting to Euler&#8217;s Number &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4151\">this month&#8217;s Exercise<\/a> is not only to calculate Euler&#8217;s number, <em>e<\/em>, but to count how many loops a program must endure before your <em>e<\/em> value calculation matches the defined constant <code>M_E<\/code>. I hope you didn&#8217;t find this challenge too difficult.<br \/>\n<!--more--><br \/>\nYes, I wanted you to write code that loops to calculate the value. Bernoulli&#8217;s equation is illustrated in Figure 1. It contains the expression <strong>lim n&rarr;\u221e<\/strong>. This mathematical dingus translates into a loop in the C language, not to infinity but to some bignum.<\/p>\n<div id=\"attachment_4157\" style=\"width: 523px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4157\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/06\/0601-figure1.png\" alt=\"\" width=\"513\" height=\"186\" class=\"size-full wp-image-4157\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/06\/0601-figure1.png 513w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/06\/0601-figure1-300x109.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/06\/0601-figure1-500x181.png 500w\" sizes=\"auto, (max-width: 513px) 100vw, 513px\" \/><p id=\"caption-attachment-4157\" class=\"wp-caption-text\">Figure 1. Bernoulli&#8217;s equation, which Euler (or Gauss) calculated to the value of <em>e<\/em>.<\/p><\/div>\n<p>Here is how I write Bernoulli&#8217;s equation in C:<\/p>\n<p><code>count = 0;<br \/>\nwhile(1)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;count++;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;e = pow( (1.0\/(double)count + 1), (double)count );<br \/>\n}<\/code><\/p>\n<p>Variable <code>count<\/code> is a <em>long<\/em> integer. It&#8217;s initialized to zero and incremented through the loop.<\/p>\n<p>The <em>pow()<\/em> function raises <code>1.0\/(double)count+1<\/code> to the <code>(double)count<\/code> power, which is the essence of Bernoulli&#8217;s equation. Of course, he put it as <code>1 + 1.0\/(double)count<\/code>, but I reversed it for clarity and precedence and fancy stuff like that.<\/p>\n<p>The <em>double<\/em> typecasts are required in the expression because using a floating point value as a looping counter is bad practice.<\/p>\n<p>The loop as presented above has no exit condition. To make one, I use the second part of the Exercise&#8217;s challenge, which is to match the value you calculate, stored in variable <code>e<\/code>, with the <code>M_E<\/code> defined constant. Here is the full code for my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_06-Exercise.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_06-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* link with -lm on Linux *\/<\/span>\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    long count;\r\n    double e;\r\n\r\n    count = 0;\r\n    while(1)\r\n    {\r\n        count++;\r\n        e = pow( (1.0\/(double)count + 1), (double)count );\r\n        if( e==M_E )\r\n            break;\r\n    }\r\n    printf(\"The computer calculated %f after %ld loops\\n\",\r\n            e,\r\n            count\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>e<\/code> holds the calculated value of Euler&#8217;s number, conveniently also called <em>e<\/em>. The <em>pow()<\/em> function within the <em>while<\/em> loop repeats, refining the value over and over as it approaches, or &#8220;tends to,&#8221; the value stored in <code>M_E<\/code>.<\/p>\n<p>Of course, the program can be written without a loop. I mean, the expression does say <strong>lim n&rarr;\u221e<\/strong>. So if you just plug in \u221e as a value, it works. Right? (Don&#8217;t blanch if you&#8217;re a mathematician.) The highest value for an <em>unsigned long int<\/em> is 4,294,967,295. Why not use it? This code does:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_06-Exercise-alt.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_06-Exercise-alt.c<\/a><\/h3>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* link with -lm on Linux *\/<\/span>\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    double e;\r\n    unsigned long count = 4294967295L;\r\n\r\n    e = pow( (1.0\/(double)count + 1), (double)count );\r\n    printf(\"The computer calculated %f for %f\\n\",\r\n            e,\r\n            M_E\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>No loop is used above. Instead, the maximum size of a <em>long int<\/em> is specified at Line 8, <code>4294967295L<\/code>, the <code>L<\/code> suffix identifying the value as a <em>long<\/em> literal. No looping. No waiting. No telling, except for the output:<\/p>\n<p><code>The computer calculated 2.718282 for 2.718282<\/code><\/p>\n<p>Presto.<\/p>\n<p>Still, the looping approach is what I wanted in your solution. If you say, &#8220;To hell with him and his silly directions&#8221; and concocted a solution similar to my alternative shown above, okay. Still, the notion was to plod through the equation, as the <strong>lim n&rarr;\u221e<\/strong> expression implies. Mathematically, it wouldn&#8217;t be any fun otherwise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is not only to calculate Euler&#8217;s number, e, but to count how many loops a program must endure before your e value calculation matches the defined constant M_E. I hope you didn&#8217;t find this &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4166\">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-4166","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\/4166","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=4166"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4166\/revisions"}],"predecessor-version":[{"id":4204,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4166\/revisions\/4204"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}