Hello, my name is Jonathan Boccara. I'm your host on Fluent C++. I have been a C++ developer for 5 years, working for Murex which is a major software editor in the finance industry. My focus is on C++ and particularly how to write expressive code. I'm happy to take your feedback, don't hesitate to drop a comment on a post, follow me or get in touch directly !
Jonathan Boccara's blog
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 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:
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 cppsamples.com 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:
0.858407 0.458407 0.141593 0.0184073 0.0143927 0.00196735 0.000611346 0.000443746
0.858407 0.0584073 0.0615927 0.0615927 0.00760735 0.00723265 0.00200865 0.000178654
0.858407 0.741593 0.101593 0.0424073 0.00720735 0.00267265 0.000272654 0.000531854
0.858407 0.0584073 0.338407 0.101593 0.0111927 0.00300735 0.00308865 0.000325746
0.858407 0.458407 0.138407 0.0375927 0.0175927 0.00731265 0.00257135 0.000252654
0.858407 0.0584073 0.0184073 0.00959265 0.0163927 0.000352654 0.00136735 0.000603346
0.858407 1.54159 0.138407 0.0775927 0.00319265 0.00431265 0.000747346 0.000276546
0.858407 0.0584073 0.181593 0.0255927 0.00320735 0.00588735 6.06536e-05 0.000811454
3.14159 0.858407 0.178407 0.0815927 0.0288073 0.00416735 0.000211346 0.000557346
0.858407 0.341593 0.0584073 0.0544073 0.000792654 0.00715265 0.000720654 0.000176146
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:
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 !     Don't want to miss out ? Follow: