Syntactic Confectionery Delight 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! :-)

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 [download]```
```   MeowChow
s aamecha.s a..a\u\$&owag.print```
```print atan2 0,-1

Create A New User
Node Status?
node history
Node Type: note [id://159407]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2017-06-25 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (567 votes). Check out past polls.