Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Golf -- compute PI

by andreychek (Parson)
on Apr 16, 2002 at 04:05 UTC ( #159407=note: print w/ replies, xml ) Need Help??


in reply to Golf -- compute PI

Here's a few more methods of achieving pi:

# 39 Characters
map$x+=(-1)**$_*4/(2*$_+1),0..1e6;die$x

Now, I know Ovid said no Trig functions, but I couldn't resist. Perhaps we could consider the next two examples mulligans :-)

# 45 Characters
use Math::Trig;die 16*atan(1/5)-4*atan(1/239)

# 21 Characters
use Math::Trig;die pi

-Eric

Update: Taking some of PrakashK's suggestions, shaved off a few more characters. Thanks PrakashK! :-)


Comment on Re: Golf -- compute PI
Replies are listed 'Best First'.
Re: Re: Golf -- compute PI
by PrakashK (Pilgrim) on Apr 16, 2002 at 04:34 UTC
    # 23 Characters
    use Math::Trig;warn pi;
    Why warn, when you can die saving one more character?
    # 22 Characters use Math::Trig;die pi;
    And, you can shave one more character, if you don't care for that pesky semi-colon at the end.

    Oh, never mind. We are not supposed to use trig functions.

    /prakash

      You can get the same result with 20 chars:
      die 3.14159265358979
      
      I found this trick in Logique, informatique et paradoxes, publisher Belin (Pour la Science), page 67, it was about the shorter program to get (not compute) pi with n digits.
(MeowChow) Re2: Golf -- compute PI
by MeowChow (Vicar) on Apr 16, 2002 at 06:23 UTC
    17 chars, if you're using trig:
      
    print 4*atan2 1,1
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://159407]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2015-07-31 11:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (276 votes), past polls