One liners to output approximations of Pi to arbitrary precision! Note: The idea is for the program to directly calculate pi rather than obfuscate it in the code somehow. I will start us off at 82, a lot of room for improvement:
@_=map(1/$_,map($_**2,1...1000000));for $a(@_){$s+=$_[$i++];}$s*=6;pri +nt sqrt($s);
Edit: Cleaned up a bit for 68:
for(@_=map(6/$_,map($_**2,1..1000000))){$s+=$_[$i++]}print sqrt($s);
Edit edit: We both realized the same thing at the same time apparently.

Replies are listed 'Best First'.
Re: Pi golf:
by ikegami (Patriarch) on Sep 05, 2006 at 20:05 UTC

    Without changing anything major, 40:

    $s+=1/$_**2for 1..1000000;print sqrt$s*6

    Note that neither this nor your solution is of arbitrary precision. Both are limited to double precision.

    Update: 38:

    $s+=6/$_**2for 1..1000000;print sqrt$s
Re: Pi golf:
by friedo (Prior) on Sep 05, 2006 at 19:50 UTC
    You can get rid of some whitespace and parens, and also the $a loop variable which you aren't using:

    @_=map(1/$_,map($_**2,1..1000000));for(@_){$s+=$_[$i++]}$s*=6;print sq +rt$s;

    Update: Ooh, and you can save even more by combining some operations:

    @_=map(1/$_,map($_**2,1..1000000));for(@_){$s+=$_[$i++]}print sqrt$s*6
      Oops, I meant to use $a originally!
Re: Pi golf:
by chargrill (Parson) on Sep 05, 2006 at 21:26 UTC

    Taking advantage of jimt's observation, 36 chars:

    $s+=6/$_**2for 1..10**6;print sqrt$s


    --chargrill
    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}
      ++chargrill; very spiffy.

      You realize of course ... changing print to die saves 2 more, and sqrt(X) is the same as X^0.5, but the latter lets you squeeze out an extra space ...

      33 chars:

      $s+=6/$_**2for 1..10**6;die$s**.5

      ... and now that I see Tanktalus' nice suggestion above, and also realizing that map saves another space over for ...

      31 chars:

      map$s+=6/$_**2,1..1e6;die$s**.5

      Update:  Come to think of it, the number 31 is nicely appropriate, given the topic (ie. 31 = int(10 * pi))


      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
        Actually, map doesn't save any space (nor does it cost any):
        $s+=6/$_**2for 1..1e6;die$s**.5 map$s+=6/$_**2,1..1e6;die$s**.5
Re: Pi golf:
by jimt (Chaplain) on Sep 05, 2006 at 21:13 UTC

    I suck at golf, but I can at least contribute a different algorithm for 66 characters:

    ($p,$o,$n)=(1,3,-1);for(1..10**6){$p+=$n/$o;$o+=2;$n=-$n}print$p*4

    Obviously, also note that 10**6 (instead of 1000000) is an easy way for the other posters here to save another 2 characters.

      As long as we're worried about individual characters ... 1e6 is shorter than 10**6 by another 2 characters ;-)

Re: Pi golf:
by ambrus (Abbot) on Sep 05, 2006 at 20:16 UTC