Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Golf -- compute PI

by lhoward (Vicar)
on Apr 16, 2002 at 01:39 UTC ( #159385=note: print w/replies, xml ) Need Help??


in reply to Golf -- compute PI

Here's my entry at 79 bytes. This is admittedly my first golf, so can probably stand some improvement.... It uses a probablistic method for determining pi. As tested below its usually accurate to 2 or 3 decimal places, but is sometimes worse. Accuracy can be increased by increasing the number of iterations.
for(1..999999){$x=rand();$y=rand();$k++if sqrt($x*$x+$y*$y)<1}print 4* +$k/999999
I managed to get it down to 61 bytes.... I'm still humbled by some of the other submissions....
for(1..1e6){$k++if sqrt(rand()**2+rand()**2)<1}print $k*4/1e6

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://159385]
help
Chatterbox?
[marto]: Sharepoint ss a mess
[marto]: /ss/is/
[Discipulus]: yes I can imagine marto but they dont take my opinion in consideration..
[marto]: I told our client this, they spent many years and lots of consultants to try and get it to do what they wanted. They're ditching it now

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















    Results (601 votes). Check out past polls.