{"id":7436,"date":"2026-03-21T00:01:44","date_gmt":"2026-03-21T07:01:44","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7436"},"modified":"2026-03-14T10:06:38","modified_gmt":"2026-03-14T17:06:38","slug":"bonus-day-2026","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7436","title":{"rendered":"Bonus &pi; Day 2026"},"content":{"rendered":"<p>The next time March 14 drops on a Saturday will be in 2037. Yes, I will be long retired by then. Or perhaps the C language will finally be retired. Who knows? Regardless, here&#8217;s another program that uses some obscure mathematical mumbo-jumbo to calculate the value of &pi;.<br \/>\n<!--more--><br \/>\nAs with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7426\">last week&#8217;s Lesson<\/a>, I use a crusty old math problem to calculate the value of &pi;. In this case, I&#8217;m working with the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Basel_problem\" target=\"_blank\">Basel problem<\/a>.<\/p>\n<p>No, Basel isn&#8217;t a human. It&#8217;s a town in Switzerland, home to the famous Bernoulli family as well as legendary mathematician Leonhard Euler. He solved this problem, which calculates the sum of the reciprocals of squares of natural numbers. His result calculated the sum to be <sup>&pi;<sup>2<\/sup><\/sup>\/<sub>6<\/sub> (&pi;<sup>2<\/sup> divided by six).<\/p>\n<p>I recognize that sticking the words &#8220;reciprocals,&#8221; &#8220;squares,&#8221; and &#8220;natural numbers&#8221; into a single sentence should be some sort of crime. To help (or not) understand what&#8217;s going on, gander at Figure 1:<\/p>\n<div id=\"attachment_7476\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7476\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0321-Figure1.png\" alt=\"\" width=\"450\" height=\"122\" class=\"size-full wp-image-7476\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0321-Figure1.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/03\/0321-Figure1-300x81.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-7476\" class=\"wp-caption-text\">Figure 1. The Basel problem.<\/p><\/div>\n<p>From the image, you see how the numerator is always one. The denominator starts at one and increments to represent the series of natural numbers (integers). Each value in the denominator is squared. As I wrote earlier, this equation doesn&#8217;t evaluate to &pi;, but rather to &pi;<sup>2<\/sup> divided by six. To obtain the value of &pi;, you must multiply the result of the series by six and then obtain its square root. I accomplish these tasks in the following code.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_03_21-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_03_21-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* compile with `-lm` to link in the math library *\/<\/span>\r\n#include &lt;locale.h&gt;\r\n#include &lt;wchar.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\n#define PI_CHAR 0x03c0\r\n#define ACCURACY 1800000\r\n\r\nint main()\r\n{\r\n    unsigned long x;\r\n    double total;\r\n\r\n    <span class=\"comments\">\/* use the Basel Problem to solve for pi *\/<\/span>\r\n    total = 0.0;\r\n    for( x=1; x&lt;=ACCURACY; x++ )\r\n    {\r\n        total += 1.0\/(x*x);\r\n    }\r\n\r\n    <span class=\"comments\">\/* output wide character 'pi' *\/<\/span>\r\n    setlocale(LC_ALL,\"\");\r\n    wprintf(L\"%lc = %f\\n\",PI_CHAR,sqrt(total*6.0) );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>As I&#8217;ve done before, this code outputs wide characters to show the symbol &pi;. It also uses two defined constants: <code>PI_CHAR<\/code> for the &pi; character, and <code>ACCURACY<\/code> to determine how long the loop spins to work through the series. The series is infinite, but the value 1,800,000 turns it enough times to generate a workable result.<\/p>\n<p>The expression representing Euler&#8217;s solution to the Basel problem is <code>total += 1.0\/(x*x);<\/code>. Variable <code>x<\/code> increments from 1 to <code>ACCURACY<\/code>. Variable <code>total<\/code> holds the cumulative value of the reciprocal of <code>x<\/code><sup>2<\/sup>, which I write as <code>x*x<\/code>. But remember that this result is <sup>&pi;<sup>2<\/sup><\/sup>\/<sub>6<\/sub>.<\/p>\n<p>The value of &pi; is calculated in the <em>printf()<\/em> statement as its final argument: <code>sqrt(total*6.0)<\/code>. Variable <code>total<\/code> is multiplied by six, then the square root is obtained. The <em>sqrt()<\/em> function requires that the <code>math.h<\/code> header file be included, but also that you link in the math library when building at the command prompt; add the <code>-lm<\/code> switch.<\/p>\n<p>The wide-character output function <em>wprintf()<\/em> is covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2593\" target=\"_blank\">another Lesson<\/a> on this blog. The <code>L<\/code> prefix creates a wide string, required for the <em>wprintf()<\/em> function&#8217;s format argument.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre>\u03c0 = 3.141592<\/pre>\n<p>This value is generated directly in the <em>printf()<\/em> statement and not stored anywhere. Still, it&#8217;s accurate enough for most calculations involving &pi;.<\/p>\n<p>If you&#8217;re curious, I&#8217;ve collected by various &pi; programs on GitHub in their own repository. <a href=\"https:\/\/github.com\/dangookin\/interesting\" target=\"_blank\">Click here<\/a> to view the lot.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Even more &pi; madness! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7436\">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-7436","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\/7436","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=7436"}],"version-history":[{"count":13,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7436\/revisions"}],"predecessor-version":[{"id":7488,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7436\/revisions\/7488"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}