Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / NewStats: 2,423,192 members, 5,437,008 topics. Date: Sunday, 23 February 2020 at 12:44 AM

The Pi Day Challenge For The Most Expressive Code In C++ - Programming - Nairaland

 The Pi Day Challenge For The Most Expressive Code In C++ by Craigston: 5:36pm On Mar 08, 2017 A Geeky Quickie for Code Slinger Types!The Pi Day Challenge For The MostExpressive Code In C++Each year, the fourteenth of March ismarked by a very special event: Pi_Day Day!It’s called this because in Americannotation, the 14th of March is 3/14. Andduring this day, the number Pi iscelebrated all over the world in variousways. One notable way to celebrate it isbringing a pie to work or to university.Another way to celebrate Pi Day is towrite expressive code. Well actually youcould very well write expressive code forother occasions like a birthday or forChristmas, or every other day of the yearfor that matter. But for celebrating Pi Day,we will launch a challenge for the mostexpressive code… about Pi!The challengeThe goal of this challenge is to write anexpressive piece of code in C++, thatcomputes an approximation of Pi. Themethod we will use is generating randompoints within a square containing a circle:The circle has a radius r, so its area is .The square has a side equal to thediameter of the circle, so . Therefore itsarea is .The probability of a point being within thecircle is the ratio of the circle area overthe square area, which is `pi(r)2 / 4r2 = pi / 4`And when a lot of points are generated, theproportion of them that fall within thecircle converges towards this probability.Therefore, the algorithm here to computePi is to create a large amount of randompoints, work out how many of them fellinto the circle, and deduce an estimate ofPi. If you don’t want to waste timesearching how to generate randomnumbers in C++, this example from cppsamples.com shows you how to do it.To spice up the challenge, we will test thismethod by making two parametersvary: the radius and the number of pointsgenerated. That is to say, you should tryout the method with different values forthe radius and for the number of points,and see how close your estimate of Pi is.The point is to find out which parameterhas more influence on the precision ofthe estimation.We’re not going to try all the possiblevalues of radius and number of points, asit would produce too much data. Rather,we will use powers of 10. So the goal hereis to make estimates of Pi with a radius of10n and by generating 10m points, with 0<= n <= 9 and 0 <= m <= 7. The outputshould show the distance between theestimates and a reference value of Pi. Thesmaller the distance, the more accuratethe estimate. A reference value you canuse for Pi is 3.14159265359.That's enough information for the challenge. Here's the web page with the original content:The Pi Day Challenge For The Most Expressive Code In C++ Re: The Pi Day Challenge For The Most Expressive Code In C++ by Craigston: 6:29pm On Mar 08, 2017 You can solve the challenge on the linked website. But homeboys would like to see your solutions. It's a simple problem, so have fun finding pi! I'll turn my solution in tonight. If someone has a more difficult challenge, please post it and mention the hommies: it's getting boring around here If I mention you unnecessarily (like the challenge being too easy for you), not bad: you still get my Pi Day greeting Cc: proxy20, jacob05, harby, laykhorn, appcypher, onedayatime, paranorman, DanielTheGeek, dhtml18, Jregz,seunthomas, FincoApps, cbrass,FrankLampard, godofbrowser,thewebcraft, Javanian, booyakasha,larisoft, stack1, yawatide, tr3y, Knownpal,rayval, blueyedgeek, jidez007, KvnqPrezo,Jenifa123, VenantCode, directonpc,DavidTheGeek, crotonite, Blenyo11,CodeNister, CodeHouse, logicalhumour,Asalimpo, lekropasky... 1 Like