Av rating:

Total votes: 9
Total comments: 12
Brain Teaser for Pi Day
02 March 2009
Alex has come up with a great idea for Pi Day. We should celebrate by trying to come up with a way, in SQL, of generating a an accurate value for Pi. If only Archimedes had possessed a laptop, his work would have been easier!
To celebrate Pi Day (14th March) we have decided to introduce a competition: Using T-SQL, find the best approximation of Pi number as a ratio of two integer numbers, both less than 1000. The ideal solution is a ratio that cannot be reduced.
Pi Day is the brainchild of the physicist Larry Shaw of San Francisco's Exploratorium, and is observed on March 14 (3/14 in American date format), due to ? being roughly equal to 3.14: At 1:59:26 pm (3.1415926) pizza and fruit pies are consumed; Celebrations can be extended: on March 14, 2004, Daniel Tammet calculated and recited 22514 decimal digits of pi.
We owe it to Archimedes that he proved that the area of a circle is equal to that of a triangle of base equal to the circumference of the circle and its height equal to the radius. The problem then became one of determining the ratio between circumference and diameter. Archimedes had to determine the value of Pi by means of seeking the limit approached by the sides of regular polygons both inscribed within, and circumscribed outside the circle. We don't know how long it took him without a laptop!
Currently we developers do not need to do it ourselves - many languages provide Pi number for us. For instance, SQL has PI() and you can use System.Math.PI in C#. However, calculating Pi as an exercise or as a brainteaser is still a fascinating way of understanding the scale of the achievements of the Ancient Greek mathematicians..
Consider the following brainteaser: using only commands, features and functions described in Books Online except for PI(), find the best approximation of Pi number as a ratio of two integer numbers, both less than 1000. The ideal solution is a ratio that cannot be reduced. You can use a helper table populated with integer numbers.
We'll reveal a solution a week after Pi Day (2009.3.14) The author of the winning solution, nominated by our distinguished panel of judges on accuracy, clarity, and ingenuity, will receive a $50 Amazon token.
This article has been viewed 5163 times.
Author profile: Alex Kuznetsov
Alex Kuznetsov has been working with object oriented languages and databases for more than a decade. He has worked with Sybase, SQL Server, Oracle and DB2. He regularly blogs on sqlblog.com, mostly about database unit testing, defensive programming, and query optimization.
Alex has published multiple articles on simple-talk.com and devx.com and wrote a book entitled "Defensive Database Programming with SQL Server". Currently he works with DRW Trading Group in Chicago, where he leads a team of developers, practicing agile development, defensive programming, and database unit testing every day. In his leisure time Alex prepares for and runs ultramarathons.
Search for other articles by Alex Kuznetsov