Advertisement
atm-irbis

Pi calculation

Apr 25th, 2014
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.36 KB | None | 0 0
  1. function gcd($a,$b) {
  2.     while($a > 0 && $b > 0) {
  3.         if($a > $b)
  4.             $a %= $b;
  5.         else
  6.             $b %= $a;
  7.     }
  8.     return $a + $b;
  9. }
  10.  
  11. function pi_calc($n) {
  12.   $m = 0;
  13.   for($i = 1; $i <= $n; $i++) {
  14.      $a = mt_rand();
  15.      $b = mt_rand();
  16.      if (gcd($a,$b) == 1) $m++;
  17.   }
  18.   return sqrt((6 * $n) / $m);
  19. }
  20.  
  21. echo pi_calc(1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement