Created
February 10, 2016 19:42
-
-
Save MidnightLightning/52b5a5286fc61392aa3d to your computer and use it in GitHub Desktop.
Compute PI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| echo bcpi(12000); | |
| function bcpi($precision){ | |
| bcscale($precision+6); | |
| $a = bcsqrt(2); | |
| $b = 0; | |
| $p = bcadd(2, $a); | |
| $i = 0; | |
| $count = ceil(log($precision+6)/log(2))-1; | |
| while($i < $count){ | |
| set_time_limit(20); // reset time limit | |
| $sqrt_a = bcsqrt($a); | |
| $a1 = $a; | |
| $a = bcdiv(bcadd($sqrt_a,bcdiv(1,$sqrt_a)),2); | |
| $b_up = bcmul($sqrt_a,bcadd(1,$b)); | |
| $b_down = bcadd($a1,$b); | |
| $b = bcdiv($b_up, $b_down); | |
| $p_up = bcmul(bcmul($p,$b),bcadd(1,$a)); | |
| $p_down = bcadd(1, $b); | |
| $p = bcdiv($p_up, $p_down); | |
| ++$i; | |
| } | |
| return bcadd($p,0,$precision); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment