in reply to Re: Re: Pi calculator
in thread Pi calculator
Running for 10,000,000 twice I got 3.1405 the first run, and 3.1424 the second. (for an average of 3.1415) It's quite likely your rand() isn't perfect. (neither is mine 10,000,000 runs should give me a digit or two more accuracy).
Anyway, here is my favorite approximation for pi, mainly because it only uses the number 2. Even though two is normally a computer friendly number, this algorithm isn't, because it also uses sqrt's. With my perl, 14 iterations is gives the maximum accuracy: 3.14159265480759
#!/usr/bin/perl w use strict; print "Enter how many iterations:\n"; chomp(my $i = <>); my $x = $i  1; my $y = sqrt(2); do { $y = sqrt(2 + $y) while ($x); $y = sqrt(2  $y); } if $x; my $z = $y * (2 ** $i); print "Pi is close to: $z\n";
So, while not the best, but I have some strange affinity to it. :)
Ciao,
Gryn
p.s. Sorry for the cryptic code for a quick decrypt its: (2**n)*sqrt(2sqrt(2+sqrt(2+sqrt(2+sqrt(2))))) with the number of 2's inside the sqrt equaling n.


Replies are listed 'Best First'.  

Re: Accuracy of Random Pi
by Fingo (Monk) on Feb 16, 2001 at 07:47 UTC  
by gryng (Hermit) on Feb 16, 2001 at 11:21 UTC  
by extremely (Priest) on Feb 16, 2001 at 12:39 UTC  
by Fingo (Monk) on Feb 16, 2001 at 16:12 UTC  
by gryng (Hermit) on Feb 16, 2001 at 18:26 UTC  
by tilly (Archbishop) on Feb 16, 2001 at 18:12 UTC  
by gryng (Hermit) on Feb 16, 2001 at 18:35 UTC  
by tilly (Archbishop) on Feb 16, 2001 at 19:28 UTC  
 
by Fingo (Monk) on Feb 17, 2001 at 01:03 UTC  
by gryng (Hermit) on Feb 19, 2001 at 19:15 UTC 
In Section
Craft