Bonus π Day 2026

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’s another program that uses some obscure mathematical mumbo-jumbo to calculate the value of π.

As with last week’s Lesson, I use a crusty old math problem to calculate the value of π. In this case, I’m working with the Basel problem.

No, Basel isn’t a human. It’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 π2/62 divided by six).

I recognize that sticking the words “reciprocals,” “squares,” and “natural numbers” into a single sentence should be some sort of crime. To help (or not) understand what’s going on, gander at Figure 1:

Figure 1. The Basel problem.

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’t evaluate to π, but rather to π2 divided by six. To obtain the value of π, you must multiply the result of the series by six and then obtain its square root. I accomplish these tasks in the following code.

2026_03_21-Lesson.c

/* compile with `-lm` to link in the math library */
#include <locale.h>
#include <wchar.h>
#include <math.h>

#define PI_CHAR 0x03c0
#define ACCURACY 1800000

int main()
{
    unsigned long x;
    double total;

    /* use the Basel Problem to solve for pi */
    total = 0.0;
    for( x=1; x<=ACCURACY; x++ )
    {
        total += 1.0/(x*x);
    }

    /* output wide character 'pi' */
    setlocale(LC_ALL,"");
    wprintf(L"%lc = %f\n",PI_CHAR,sqrt(total*6.0) );

    return 0;
}

As I’ve done before, this code outputs wide characters to show the symbol π. It also uses two defined constants: PI_CHAR for the π character, and ACCURACY 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.

The expression representing Euler’s solution to the Basel problem is total += 1.0/(x*x);. Variable x increments from 1 to ACCURACY. Variable total holds the cumulative value of the reciprocal of x2, which I write as x*x. But remember that this result is π2/6.

The value of π is calculated in the printf() statement as its final argument: sqrt(total*6.0). Variable total is multiplied by six, then the square root is obtained. The sqrt() function requires that the math.h header file be included, but also that you link in the math library when building at the command prompt; add the -lm switch.

The wide-character output function wprintf() is covered in another Lesson on this blog. The L prefix creates a wide string, required for the wprintf() function’s format argument.

Here’s sample output:

π = 3.141592

This value is generated directly in the printf() statement and not stored anywhere. Still, it’s accurate enough for most calculations involving π.

If you’re curious, I’ve collected by various π programs on GitHub in their own repository. Click here to view the lot.

Leave a Reply