The Pi Day Challenge For The Most Expressive Code In C++
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:
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:
- 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?
The challenge is now closed. Find here the outcome of the challenge.
Here are the submissions – thanks to all the participants!
Jack Z: https://gist.github.com/
Alex White: https://gist.github.com/jaked122/51e619da2581c13593528fba73177aa8
Paul Dreik: https://gist.github.com/anonymous/e24fce8f800ad7dc48a32e117db660e8
mjkoo: https://gist.github.
Ryan Zischke: https://gist.github.com/zisc/00d95734d070e00694f51875013baa51
Valeri Kim: https://gist.github.com/anonymous/e09a2d11741aa54ddadb7e83bedc0540
Ben Steffan: https://gist.github.com/BiCapitalization/dd4b21a875fd44a644cf8d90fdc4f2a6
Balázs Árva: https://gist.github.com/arvabalazs/cfa0b80c26f0c95b57d5e7436674fd8f
Ryan Phelps: https://gist.github.com/ryancerium/76b95e7ce4a8520ca8b06e193e84099c
BrandtM: https://gist.github.com/BrandtM/7b6d5273ed5ccc4df9ec59633587c909
vlovo: https://gist.github.com/vlovo/2aa6c746d40b176b7f7cb0717448f45d
Arnaud Brejeon: https://gist.github.com/arnaudbrejeon/acb13cdd7fb3c87961e3d7b9e632dac6
Marcin: https://gist.github.com/mzdun/b1376203c57f54d2ceecebdb26327974
Jef42: https://gist.github.com/Oroles/ee51f79e699ebca89a4731ae052b8890
Philippe Baucour 1: https://gist.github.com/40tude/223cec0234d17938648a478393eabe89
Philippe Baucour 2: https://gist.github.com/40tude/3c8d041ee3841180e313fb2e48f6ba6b
William Killian: https://gist.github.com/willkill07/3c77df53f43fbed4c796585c1cf99e5d <= this is our winner
Cedric Meriadec: https://gist.github.com/anonymous/e12bdda4299814180cafffa341fdf9ca
Arne Mertz: https://gist.github.com/arnemertz/5c2a049c2260c9539f43534aec1fcbaa
FredTingaud: https://gist.github.com/FredTingaud/5c6c7890f11abc07b9ad44dd68970150
Ben Deane: https://gist.github.com/elbeno/aace93b23510450ef95c0cb45120cf37
Kjell-Olov Högdahl 1: https://gist.github.com/kjelloh/204121dd4ec036b7a62d28da78628fcc
Kjell-Olov Högdahl 2: https://gist.github.com/kjelloh/6b08835e362bcc55ae781797c472f91d
Kjell-Olov Högdahl 3: https://gist.github.com/kjelloh/d787f6b7e2c56c5e1932144ce8f8fcea
Vineeth Viswan: https://gist.github.com/vineethviswan/2d5790ba6a5e171d3c74b3fac84221fd
Marek Krudej: https://gist.github.com/mkurdej/b0697cd751407d7e142083263733c88f
Jungwoo Yang: https://gist.github.com/yjwoo14/61b4872582e68f8307ae47d7f50337c4
Kirit Sælensminde: https://gist.github.com/KayEss/e250cec9d5d4bc423087bcc6c11cb7bf
Vaughn Cato: https://gist.github.com/vcato/4fdecd703261fb17bc843905017e9ac0
Jonathan Barisere: https://gist.github.com/latentgenius/d706c66e908ff114a323010cf1b234ea
Masako Toda: https://gist.github.com/masakotoda/95c8d2e6947bfe73048e165ade719a3c
Don't want to miss out ? Follow:   
Share this post!