用PHP计算圆周率的一个算法

2022年12月24日 1802点热度 0人点赞 0条评论
<?
$start_time = microtime(true);
$n=5000; // 圆周率的长度
$w = $n+10;
$b =bcpow(10,$w);
$rtemp=bcmul($left=$b, $right=4);
$x1 =bcdiv($left=$rtemp, $right=5);
$x2 =bcdiv($left=$b, $right=-239);
$he=bcadd($left=$x1, $right=$x2);
$n =$n*2;
for ($i=3; $i<$n;  $i =$i+2 ) {
    $x1 =bcdiv($left=$x1, $right=-25);
    $x2 =bcdiv($left=$x2, $right=-57121);
    $x =bcdiv($left=bcadd($left=$x1, $right=$x2), $right=$i);
    $he=bcadd($left=$he, $right=$x);
}
$pai =bcmul($left=$he, $right=4);
$pai =bcdiv($left=$pai, $right=bcpow(10,10));
echo $pai;
echo "\n";
$end_time = microtime(true);
echo '耗时'.round($end_time-$start_time,5).'秒<br>';

路灯

这个人很懒,什么都没留下

文章评论