Friday Puzzler: Pi - it's what's for dinner

This post is more than 2 years old.

Today's Friday Puzzler is one that I almost skipped out on. It seems too simple. But the more I thought about it, the more I thought it might be fun to actually see what people do with it. And since most of my supposedly "5 minute puzzlers" end up not being that quick, hopefully this one will make up for it. Being that I think this one is a bit simpler, the winner of the contest will be based purely on how cool you display the results. Use HTML, use JavaScript, use PDFs with blinking lights, I don't care. Go crazy. Your coding contest this morning is - build a utility that returns the digits of Pi up to N significant digits. So given func(2), the result should be 14. Given func(4), it would be 1415. It should work from any number up to 1,000,000. And if you create a solution that can do a million and render it quickly, you get bonus points.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Micah posted on 12/2/2011 at 8:18 PM

I know this isn't part of this contest exactly but while looking around on pi I found this really interesting script. It allows you to enter a set of numbers and it tells you (really quickly) the location in pi (up to 200 million positions) where that number set can be located.
http://www.angio.net/pi/big...

I just thought this was really cool and wanted to share with the class. Then again, I am very easily amused.

Comment 2 by Scott Stroz posted on 12/2/2011 at 9:11 PM

I took a quick stab at this, but the Pi() function (and the value in java.lang.Math) only return the value of Pi to 11 decimal places. I will post different solution if/when I find how to express Pi in more decimals.

<cfscript>
function PieceOfPi( Numeric val){
return( left( listLast( Pi(), "." ), int( val ) ) );
}
</cfscript>

Comment 3 by Raymond Camden posted on 12/2/2011 at 9:25 PM

You fail. Because.

(Sorry, private joke.)

Comment 4 by Raymond Camden posted on 12/2/2011 at 9:25 PM

Ok, on a serious note, you win because of the name itself. Just noticed that. #perfect

Comment 5 by Scott Stroz posted on 12/2/2011 at 9:50 PM

Found a representation if Pi to 1,000,000 decimal places but cannot post code here as its way too big (I tried and it displayed poorly since most was not visible).

Here is a link to the code in Google Doc - https://docs.google.com/doc...

Be warned, 1) this code executes slowly and b) I make no guarantee how long this file will be available.

Comment 6 by Joshua Miller posted on 12/3/2011 at 12:30 AM

Slices of Pi
My solution displays a pie chart (clever, huh?) that indicates the frequency of occurrence of each digit in Pi to 1,000,000 places. Uses Google Visualizations to render the graph; the data for Pi is from a remote source.

Source Code: http://pastebin.com/TfdyLPbv
Output: http://twitpic.com/7n7xn6

Comment 7 by Raymond Camden posted on 12/3/2011 at 12:33 AM

Damn! That's really interesting! Thanks.

Comment 8 by Ben Nadel posted on 12/3/2011 at 12:53 AM

I love the Pi chart :D It's so strange how even the distribution of numbers is in the value. It probably makes perfect sense... just odd to see.

Comment 9 by Joshua Miller posted on 12/3/2011 at 12:57 AM

I know what you mean about the distribution, for some reason I expected to see a disproportionate quantity of odd numbers. Even more strange is how the distribution holds true for 100, 1000, 100000, 1000000, places, etc. If I were a math guy I'd probably laugh at my lack of understanding of why this is, but ignorance is bliss.