Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 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

Nairaland Forum / Science/Technology / Programming / The Pi Day Challenge For The Most Expressive Code In C++ (398 Views)

Please,Help Explain This Code In C / I Need Online Banking Source Code In PHP, MSQL / Online Banking Source Code In PHP, MSQL (2) (3) (4)

(1) (Reply)

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 Most
Expressive Code In C++


Each year, the fourteenth of March is
marked by a very special event: Pi_Day 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
pi(r)2 / 4r2 = pi / 4



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.



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 sad


If I mention you unnecessarily (like the challenge being too easy for you), not bad: you still get my Pi Day greeting smiley


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

(1) (Reply)

Need Of IT Training Center / Online Tv For You / Ajax Search Box In PHP, Mysql And Jquery.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2020 Oluwaseun Osewa. All rights reserved. See How To Advertise. 41
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.