Jonathan Boccara's blog

The Pi Day Challenge For The Most Expressive Code In C++

Published 3 weeks ago - 42 Comments

Each year, the fourteenth of March is marked by a very special event: Pi Day! It’s called this because in American notation, the 14th of March is 3/14. And during this day, the number Pi is celebrated all over the world in various ways. One notable way to celebrate it is bringing a pie to work or to university.

Another way to celebrate Pi Day is to write expressive code. Well actually you could very well write expressive code for other occasions like a birthday or for Christmas, or every other day of the year for that matter. But for celebrating Pi Day, we will launch a challenge for the most expressive code… about Pi!

The challenge

The goal of this challenge is to write an expressive piece of code in C++, that computes an approximation of Pi. The method we will use is generating random points within a square containing a circle:

Pi Day challenge

The circle has a radius r, so its area is .

The square has a side equal to the diameter of the circle, so . Therefore its area is .

The probability of a point being within the circle is the ratio of the circle area over the square area, which is . And when a lot of points are generated, the proportion of them that fall within the circle converges towards this probability.

Therefore, the algorithm here to compute Pi is to create a large amount of random points, work out how many of them fell into the circle, and deduce an estimate of Pi. If you don’t want to waste time searching how to generate random numbers in C++, this example from shows you how to do it.

To spice up the challenge, we will test this method by making two parameters vary: the radius and the number of points generated. That is to say, you should try out the method with different values for the radius and for the number of points, and see how close your estimate of Pi is. The point is to find out which parameter has more influence on the precision of the estimation.

We’re not going to try all the possible values of radius and number of points, as it would produce too much data. Rather, we will use powers of 10. So the goal here is to make estimates of Pi with a radius of 10n and by generating 10m points, with 0 <= n <= 9 and 0 <= m <= 7. The output should show the distance between the estimates and a reference value of Pi. The smaller the distance, the more accurate the estimate. A reference value you can use for Pi is 3.14159265359.

Here is an example of output:

Say that the rows correspond to the various powers of 10 for radius: the first row has radius 1, the second one has radius 10, the third one has radius 100, and so on. And the columns correspond to the various powers of 10 for the number of points generated. And let’s not worry about lines headers and column headers here, in order to focus on the rest of the algorithm.

If you feel up to this, fhe challenge consists in writing a piece of code that fills two conditions:

  • it should work! That is it should output the precision errors with the various radii and number of points,
  • it should be as expressive as possible. This is actually the whole point of the challenge.

To submit a solution, make a gist and post the link in the comment section below this post. If you’ve never used gist before, it’s dead easy: just  go to gist, write your code, click on “Create public gist”, and copy paste the generated link in the comment section of this post.

Include a way for me to contact you, if you win and would like your website, blog, Twitter handle, or whichever piece of work to be put forward in the challenge solution. If you don’t want to put your email in your public solution, then you can sign up to the newsletter below so that I only can have it (it’s completely free and you can unsubscribe at any time), or if you don’t want to join the mailing list you can still send me your email privately.

Your solution can be submitted before the 13th of March 2017. Then on Pi day, we will know who the winner of the expressive code challenge is, and one solution to this challenge will be described in a dedicated post. Make sure you sign up to the newsletter below if you want to be notified when the results of the challenge come out.

If there is anything unclear, you just let me know.

Oh and, if for any reason you’re not in the mood for coding right now, why not post a comment to tell us how YOU celebrate Pi Day?

Liked it ? Share this post ! Facebooktwittergoogle_plus    Don't want to miss out ? Follow:   twitterrss

Subscribe to the newsletter