I'm ignoring the fact that this code is useless,it's really slow and there are23 # 24 # pi*($d/2)**2 in circle 25 # ------------ ~ ----------- ~ pi/4 26 # $d**2 in total 27 # 28 29 30 31 use strict; 32 use warnings; 33 34 my $d = 1000;#diameter 35 my $no_total = 1000000;#number of points 36 my $no_in=0; 37 38 sub gen_xy { ( int(rand($d)),int(rand($d)) ); } 39 40 sub in_circle { 41 my ($x,$y) = @_; 42 ($x - $d/2)**2 + ($y - $d/2)**2 <= ($d/2)**2 43 ? 1 44 : 0; 45 } 46 47 48 $no_in += in_circle gen_xy 49 for 1..$no_total; 50 51 warn "Pi approx: ". ( $no_in/$no_total ) *4 ;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: probabilistic pi algorithm
by FunkyMonk (Bishop) on Feb 15, 2008 at 15:58 UTC | |
by bart (Canon) on Feb 16, 2008 at 09:30 UTC | |
by spx2 (Deacon) on Feb 16, 2008 at 09:34 UTC | |
by FunkyMonk (Bishop) on Feb 16, 2008 at 10:07 UTC | |
by starbolin (Hermit) on Feb 20, 2008 at 06:35 UTC |