{"id":4151,"date":"2020-06-01T00:01:16","date_gmt":"2020-06-01T07:01:16","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4151"},"modified":"2024-03-27T16:18:24","modified_gmt":"2024-03-27T23:18:24","slug":"getting-to-eulers-number","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4151","title":{"rendered":"Getting to Euler&#8217;s Number"},"content":{"rendered":"<p>I enjoy reading about and studying mathematics. One of my favorite books is <em><a href=\"https:\/\/amzn.to\/3dTRJb4\" rel=\"noopener noreferrer\" target=\"_blank\">A History of &pi;<\/a><\/em>. I&#8217;ve used this book to inspire my Pi Day (March 14) programs. It&#8217;s fascinating stuff.<br \/>\n<!--more--><br \/>\nWhat I don&#8217;t enjoy about mathematics, of course, is doing the math. This conflict is resolved by programming, where I can attempt to pound out a formula and then let the computer toil with the calculations. The compiler is more forgiving than any math instructor, showing me where I&#8217;ve erred and giving me a chance to try again.<\/p>\n<p>For this month&#8217;s challenge, the topic is <a href=\"https:\/\/en.wikipedia.org\/wiki\/E_(mathematical_constant)\" rel=\"noopener noreferrer\" target=\"_blank\">Euler&#8217;s number<\/a>, the mathematical constant <em>e<\/em>. Once again, it&#8217;s fascinating to read about this value and even watch YouTube videos (<a href=\"https:\/\/www.youtube.com\/watch?v=AuA2EAgAegE\" rel=\"noopener noreferrer\" target=\"_blank\">this one is excellent<\/a>) on how it works and its history.<\/p>\n<p>Figure 1 illustrates the <em>e<\/em> equation, which was discovered by Italian mathematician Jacob Bernoulli in 1683. The result was calculated by Euler as the value 2.71828182845904523536028747135266250 or thereabouts. It&#8217;s one of those magic numbers like &pi; that&#8217;s transcendental and irrational &mdash; and lots of fun!<\/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=\"Calculating 'e'\" 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>In the C language, the <em>e<\/em> constant is defined in the <code>math.h<\/code> header file, thus:<\/p>\n<p><code>#define M_E 2.71828182845904523536028747135266250<\/code><\/p>\n<p>Yes, it&#8217;s that important.<\/p>\n<p>Your challenge is to write code that calculates the value <em>e<\/em>. It&#8217;s not really that difficult, once you unwind the formula shown in Figure 1 and translate it into C code. But that&#8217;s not all! No, you must run and run your the code through a loop in order to obtain the most accurate representation of the value <em>e<\/em>.<\/p>\n<p>How do you know your calculation is a &#8220;most accurate representation&#8221;? As your code calculates the value of <em>e<\/em>, compare your result with the defined constant <code>M_E<\/code>. The program shouldn&#8217;t finish its calculations until both your value and the <code>M_E<\/code> constant match.<\/p>\n<p>But wait! There&#8217;s more . . .<\/p>\n<p>As your code loops, count the iterations. Once the program arrives at the same value as defined constant <code>M_E<\/code>, output the number of loops it took to make the match. Here&#8217;s output from my solution:<\/p>\n<p><code>The computer calculated 2.718282 after 220825296 loops<\/code><\/p>\n<p><strong>TIP:<\/strong> Some platforms require that you link in the math library if you use any C language math functions. The command line switch to do so is <code>-lm<\/code> (little L, little M).<\/p>\n<p>If you&#8217;re using a terminal window to run your solution, use the <em>time<\/em> command to see how long it takes the computer to arrive at the solution. Here is the output on my iMac Pro running a 3.2GHz 8-Core Intel Xeon W:<\/p>\n<pre class=\"screen\">\r\nThe computer calculated 2.718282 after 220825296 loops\r\n\r\nreal\t0m6.678s\r\nuser\t0m6.672s\r\nsys\t0m0.004s<\/pre>\n<p>Here is the output on a PC (Intel NUC) running a 3.1GHz Core i7:<\/p>\n<pre class=\"screen\">\r\nThe computer calculated 2.718282 after 220825296 loops\r\n\r\nreal\t0m9.696s\r\nuser\t0m9.688s\r\nsys\t0m0.016s<\/pre>\n<p>There! I made my PC feel like crap all day! Perhaps you can do the same!<\/p>\n<p>Please try this Exercise on your own before you view <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4166\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How long does it take for a program to reach the <em>e<\/em> constant? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4151\">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-4151","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\/4151","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=4151"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4151\/revisions"}],"predecessor-version":[{"id":6337,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4151\/revisions\/6337"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}