Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Golf -- compute PI

by pepik_knize (Scribe)
on Apr 16, 2002 at 15:54 UTC ( #159541=note: print w/replies, xml ) Need Help??


in reply to Golf -- compute PI

Here's one using Archimedes's method that comes in at 67:

$n=3;$s=1;for(1..9){$n*=2;$s=(2-(4-$s**2)**.5)**.5;$p=$n*$s}print$p

As a side note, I had $_ instead of $p inside the curlies, but using just print outside didn't work, and I don't know why.

Pepik

Replies are listed 'Best First'.
Re: Re: Golf -- compute PI
by Ovid (Cardinal) on Apr 16, 2002 at 16:54 UTC

    Using $_ instead of $p is a nice try to save two strokes, but it fails because for(1..9){..} aliases elements of the range to $_, which overwrites your later use of $p. Good job, though.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Re: Golf -- compute PI
by blakem (Monsignor) on Apr 17, 2002 at 06:41 UTC
    You could use $\ instead, which will trim the two chars at the expense of an 'uninitialized' warning....
    $n=3;$s=1;for(1..9){$n*=2;$s=(2-(4-$s**2)**.5)**.5;$\=$n*$s}print
    Infact, if you rearrange things and toss in a little obfu, you can trim a few more chars. It even has the side-effect of making it strict compliant:
    $@=3;$;=1;$@*=2,$;=(2-(4-$;**2)**.5)**.5,$\=$@*$;for 1..9;print

    -Blake

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2022-08-19 17:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?