# Code Golf Challenge : 1,000 Digits Of Pi

**URL:** <https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050>\
**Category:** Ruby\
**Created:** [September 18, 2006, 12:33pm UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050 "2006-09-18T12:33:27Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 18, 2006, 12:33pm UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/1 "2006-09-18T12:33:27Z")

</div>

Hi all,

Code Golf’s 12th challenge has just been added to the site. It asks you  
to calculate the first 1,000 digits of Pi - Something I’m sure most of  
you have thought about, but never done. You can see the challenge at :

[http://codegolf.com/1000-digits-of-pi](http://codegolf.com/1000-digits-of-pi)

For those who haven’t heard of [codegolf.com](http://codegolf.com), it can be described as  
“allowing you to show off your code-fu by trying to solve coding  
problems using the least number of keystrokes.”

The site currently supports Perl, PHP, Python and Ruby with Scheme to  
(hopefully) follow soon. Your entries are automatically executed and  
scored - No need to wait for someone to validate your code.

Other challenges include implementing the Vigenere Cipher, Prime  
factors, SHA-256 Hashing as well as the obvious 99 Bottles Of Beer. All  
of the challenges are competitive but there is still scope to well in  
them, even if you’re new to the site.

I hope some of you will find this interesting, and sorry for disturbing  
you if not!

Have fun,  
Carl.

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 18, 2006, 3:55pm UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/2 "2006-09-18T15:55:55Z")

</div>

On Sep 18, 2006, at 5:33 AM, Carl D. wrote:

> Hi all,
> 
> Code Golf’s 12th challenge has just been added to the site. It  
> asks you to calculate the first 1,000 digits of Pi - Something I’m  
> sure most of you have thought about, but never done. You can see  
> the challenge at :
> 
> [http://codegolf.com/1000-digits-of-pi](http://codegolf.com/1000-digits-of-pi)

Darn it! They blocked my attempt to use open-uri. How’s a guy  
suppose to cheat with these restrictions. Sheesh!

James Edward G. II

require"open-uri";puts"3.“+open(“[http://www.joyofpi.com/](http://www.joyofpi.com/)  
pi.html”).read.gsub(/\s/,”")[/\d{1000}/]

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 18, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/3 "2006-09-18T16:21:59Z")

</div>

On 9/18/06, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Darn it! They blocked my attempt to use open-uri. How’s a guy  
> suppose to cheat with these restrictions. Sheesh!
> 
> James Edward G. II
> 
> require"open-uri";puts"3.“+open(“[http://www.joyofpi.com/](http://www.joyofpi.com/)  
> pi.html”).read.gsub(/\s/,”")[/\d{1000}/]

idk James, that might change it from an exercise in code-fu into an  
exercise in who-has-the-shortest-domain-name-fu…

Anyone have any idea how they’re safely running the code on the server?

Freaky freaky sandbox action?

-Harold

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 18, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/4 "2006-09-18T16:26:00Z")

</div>

Hi,

> > [http://codegolf.com/1000-digits-of-pi](http://codegolf.com/1000-digits-of-pi)
> 
> Darn it! They blocked my attempt to use open-uri. How’s a guy suppose  
> to cheat with these restrictions. Sheesh!

We wouldn’t want to make it too easy for you!

> require"open-uri";puts"3."+open(“[http://www.joyofpi.com/pi.html").read.gsub(/\s/,"](http://www.joyofpi.com/pi.html%22).read.gsub(/%5Cs/,%22)”)[/\d{1000}/]

Believe it or not, someone is 30-odd bytes better than that and he’s  
(presumably) actually calculating the digits!

Regards,  
Carl.

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 18, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/5 "2006-09-18T16:28:01Z")

</div>

Hi Harold,

> Anyone have any idea how they’re safely running the code on the server?
> 
> Freaky freaky sandbox action?

No, Freaky freaky jail-and-chroot action - We can support pretty much  
any language that way. I might finally get round to writing an article  
about how we’re doing it, but I’ve been saying that for months, so I  
wouldn’t hold my breath!

Regards,  
Carl.

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 18, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/6 "2006-09-18T16:30:08Z")

</div>

On Sep 18, 2006, at 5:33 AM, Carl D. wrote:

> Hi all,
> 
> Code Golf’s 12th challenge has just been added to the site. It  
> asks you to calculate the first 1,000 digits of Pi - Something I’m  
> sure most of you have thought about, but never done. You can see  
> the challenge at :
> 
> [http://codegolf.com/1000-digits-of-pi](http://codegolf.com/1000-digits-of-pi)

Another blocked cheat! These people clearly do not appreciate  
cheating as they should. 😉

James Edward G. II

#!ruby -rbigdecimal -rbigdecimal/math  
include BigMath;puts"3."+BigDecimal.PI(t=1000).to\_s[3,t]

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 19, 2006, 2:03am UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/7 "2006-09-19T02:03:24Z")

</div>

On Mon, Sep 18, 2006 at 07:33:57PM +0900, Carl D. wrote:

> (…)  
> I hope some of you will find this interesting, and sorry for disturbing  
> you if not!

Code golfing is not for me, yet watching others do is very amusing.  
So when will the submitted entries be open to public ?

-Martin

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 19, 2006, 1:57am UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/8 "2006-09-19T01:57:24Z")

</div>

On Mon, 18 Sep 2006, James Edward G. II wrote:

> Another blocked cheat! These people clearly do not appreciate cheating as  
> they should. 😉
> 
> James Edward G. II
> 
> #!ruby -rbigdecimal -rbigdecimal/math  
> include BigMath;puts"3."+BigDecimal.PI(t=1000).to\_s[3,t]

Indeed they should appreciate it…  
[Crypto-gram: September 15, 2006 - Schneier on Security](http://www.schneier.com/crypto-gram-0609.html#12)

John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand

“We have more to fear from  
The Bungling of the Incompetent  
Than from the Machinations of the Wicked.” (source unknown)

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 19, 2006, 5:09am UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/9 "2006-09-19T05:09:54Z")

</div>

On 9/18/06, Martin S. Weber [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

They are still discussing it on their forums, but I just looked today  
and tried out the 99 bottles of beer. I have the shortest Ruby (202  
bytes), but I am only tied for 10th place overall. The best Perl entry  
is kicking my butt at 176 bytes. I think this is probably due to the  
slightly more verbose string interpolation in Ruby, but I am  
interested in what could be done to further shrink my script:

@a=‘#{n} bottle#{n\>1?:s:“”} of beer’  
@b=" on the wall"  
(n=99).times{puts eval""#@a#@b, #@a.\n#{n-=1;n\<1?(n=99;‘Go to the  
store and buy some more’):‘Take one down and pass it around’},  
#@a#@b.\n\n""}

pth

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 19, 2006, 2:00pm UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/10 "2006-09-19T14:00:41Z")

</div>

Wow, ya’ll are good code golfers! Of the few challenges I’ve been able  
to solve so far, my solutions are much bigger than the winners (about  
in the middle from smallest to largest). I guess I’m not gonna be the  
Tiger Woods of code golfing. My best effort on the 99 bottles challenge  
is 243 bytes 😉

i="%d bottle%s of beer"  
j=" on the wall"  
k="\nTake one down and pass it around, "  
l="\nGo to the store and buy some more, "  
99.downto(1){|x|m=x-1\>0?[k,x-1]:[l,99];n=i%[x,(:s if x\>1)];  
puts n+j+’, ‘+n+’.’+m[0]+i%[m[1],(:s if m[1]\>1)]+j+".\n\n"}

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 19, 2006, 10:52am UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/11 "2006-09-19T10:52:25Z")

</div>

Hi Patrick,

> They are still discussing it on their forums, but I just looked today  
> and tried out the 99 bottles of beer. I have the shortest Ruby (202  
> bytes), but I am only tied for 10th place overall. The best Perl entry  
> is kicking my butt at 176 bytes. I think this is probably due to the  
> slightly more verbose string interpolation in Ruby, but I am  
> interested in what could be done to further shrink my script:

Very good attempt! Unfortunately, the shortest Ruby is 186 bytes, but  
both people who have got that score have better solutions in Perl, so  
their Ruby entries don’t show on the Overall Leaderboard. (Each  
language is scored individually as well as there being an overall  
leaderboard.)

> @a=‘#{n} bottle#{n\>1?:s:“”} of beer’  
> @b=" on the wall"  
> (n=99).times{puts eval""#@a#@b, #@a.\n#{n-=1;n\<1?(n=99;‘Go to the  
> store and buy some more’):‘Take one down and pass it around’},  
> #@a#@b.\n\n""}

You can save three bytes by including a literal newline instead of \n :

[Parked at Loopia](http://pastie.caboo.se/13961)

I’m sure you can shave more bytes of it!

Regards,  
Carl.

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 19, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/12 "2006-09-19T16:45:36Z")

</div>

Carl D. wrote:

> I tell you what though, if obfuscated code were the aim, you would  
> surely win!

Hehe! You should see some of my javascript bookmarklets! It’s kind of  
fun to break all the rules of good programming though.

> I like the use of :s to get the “s” to pluralise the  
> bottle, I’m sure I could use that somewhere.

I actually gipped that from Patrick’s code above (i.e., the ternary in  
@a).

> I don’t know what tips to give you really, other that your code doesn’t  
> need to be that complicated! Stick in there though, it takes a bit of a  
> mindset change to golf.

Thanks! I don’t think I’m clever enough to beat most of the folks, but  
it’s fun to try. 🙂

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 19, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/13 "2006-09-19T21:53:03Z")

</div>

On 9/18/06, Carl D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (presumably) actually calculating the digits!

I got a 64 bytes version now, based on the “tiny c program” from  
[http://www.boo.net/~jasonp/pipage.html](http://www.boo.net/~jasonp/pipage.html)  
And it does calculate the digits, although i’m not yet sure how 🙂

I don’t care much for these rankings, so what would be a nice time to  
post this without spoiling it for others?

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 19, 2006, 3:38pm UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/14 "2006-09-19T15:38:53Z")

</div>

Hey Jordan,

> 99.downto(1){|x|m=x-1\>0?[k,x-1]:[l,99];n=i%[x,(:s if x\>1)];  
> puts n+j+’, ‘+n+’.’+m[0]+i%[m[1],(:s if m[1]\>1)]+j+".\n\n"}

I tell you what though, if obfuscated code were the aim, you would  
surely win! I like the use of :s to get the “s” to pluralise the  
bottle, I’m sure I could use that somewhere.

I don’t know what tips to give you really, other that your code doesn’t  
need to be that complicated! Stick in there though, it takes a bit of a  
mindset change to golf.

C.

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 19, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/15 "2006-09-19T22:55:11Z")

</div>

On 9/18/06, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Darn it! They blocked my attempt to use open-uri. How’s a guy  
> suppose to cheat with these restrictions. Sheesh!
> 
> James Edward G. II
> 
> require"open-uri";puts"3.“+open(“[http://www.joyofpi.com/](http://www.joyofpi.com/)  
> pi.html”).read.gsub(/\s/,”")[/\d{1000}/]

Haha, ever the cheater, James G.!

---

<div class="post-metadata">

**Author:** ![Carl\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@Carl\_D](https://www.ruby-forum.com/u/Carl_D)\
**Post date:** [September 19, 2006, 11:13pm UTC](https://www.ruby-forum.com/t/code-golf-challenge-1-000-digits-of-pi/69050/16 "2006-09-19T23:13:13Z")

</div>

On Wed, 20 Sep 2006, Leslie V. wrote:

> > > [http://codegolf.com/1000-digits-of-pi](http://codegolf.com/1000-digits-of-pi)
> > 
> > Darn it! They blocked my attempt to use open-uri. How’s a guy  
> > suppose to cheat with these restrictions. Sheesh!
> > 
> > James Edward G. II
> > 
> > require"open-uri";puts"3.“+open(“[http://www.joyofpi.com/](http://www.joyofpi.com/)  
> > pi.html”).read.gsub(/\s/,”")[/\d{1000}/]
> 
> Haha, ever the cheater, James G.!

i tried that too. i figure they’ve munged $:, but a concerted effort to  
use

load absolute\_path\_to\_open\_uri\_rb

may get it. 😉

-a
