当前位置:Gxlcms > PHP教程 > 使用BBP公式计算Pi的代码

使用BBP公式计算Pi的代码

时间:2021-07-01 10:21:17 帮助过:22人阅读

BBP公式号称可以直接获取Pi的第N位结果,本来我以为这种获取的速度在任意一个位置都是相同的呢,但是从bellard的主页(http://bellard.org/pi/)下载一个改进的C程序测试后发现N越大,计算时间越长,我转换成php代码后,计算第1K位时会超过1分钟的时间,原C程序计算的时候也是很慢

这个程序一次只会输出9个数字(囧rz,不知道怎样改能多输出几位)
  1. /**
  2. * 圆周率计算(BBP)
  3. * @author Moyo
  4. * @url http://moyo.uuland.org/code/php-pi-calc/
  5. * @version 1.0
  6. * @date 2013.01.12
  7. */
  8. class pi
  9. {
  10. public static function calc($__N__)
  11. {
  12. $n = (int)$__N__;
  13. $av = $a = $vmax = $N = $num = $den = $k = $kq = $kq2 = $t = $v = $s = $i = 0;
  14. $sum = 0.0;
  15. $N = (int)(($n + 20) * log(10) / log(2));
  16. $sum = 0;
  17. for ($a = 3; $a <= (2 * $N); $a = self::next_prime($a))
  18. {
  19. $vmax = (int)(log(2 * $N) / log($a));
  20. $av = 1;
  21. for ($i = 0; $i < $vmax; $i ++)
  22. {
  23. $av = ($av * $a);
  24. }
  25. $s = 0;
  26. $num = 1;
  27. $den = 1;
  28. $v = 0;
  29. $kq = 1;
  30. $kq2 = 1;
  31. for ($k = 1; $k <= $N; $k ++)
  32. {
  33. $t = $k;
  34. if ($kq >= $a)
  35. {
  36. do
  37. {
  38. $t = (int)($t / $a);
  39. $v --;
  40. }
  41. while (($t % $a) == 0);
  42. $kq = 0;
  43. }
  44. $kq ++;
  45. $num = self::mul_mod($num, $t, $av);
  46. $t = (2 * $k -1);
  47. if ($kq2 >= $a)
  48. {
  49. if ($kq2 == $a)
  50. {
  51. do
  52. {
  53. $t = (int)($t / $a);
  54. $v ++;
  55. }
  56. while (($t % $a) == 0);
  57. }
  58. $kq2 -= $a;
  59. }
  60. $den = self::mul_mod($den, $t, $av);
  61. $kq2 += 2;
  62. if ($v > 0)
  63. {
  64. $t = self::inv_mod($den, $av);
  65. $t = self::mul_mod($t, $num, $av);
  66. $t = self::mul_mod($t, $k, $av);
  67. for ($i = $v; $i < $vmax; $i ++)
  68. {
  69. $t = self::mul_mod($t, $a, $av);
  70. }
  71. $s += $t;
  72. if ($s >= $av)
  73. {
  74. $s -= $av;
  75. }
  76. }
  77. }
  78. $t = self::pow_mod(10, ($n - 1), $av);
  79. $s = self::mul_mod($s, $t, $av);
  80. $sum = (double)fmod((double)$sum + (double)$s / (double)$av, 1.0);
  81. }
  82. return array(
  83. 'n' => $n,
  84. 'v' => sprintf('%09d', (int)($sum * 1e9))
  85. );
  86. }
  87. private static function next_prime($n)
  88. {
  89. do
  90. {
  91. $n ++;
  92. }
  93. while (!self::is_prime($n));
  94. return $n;
  95. }
  96. private static function is_prime($n)
  97. {
  98. $r = $i = 0;
  99. if (($n % 2) == 0)
  100. {
  101. return 0;
  102. }
  103. $r = (int)(sqrt($n));
  104. for ($i = 3; $i <= $r; $i += 2)
  105. {
  106. if (($n % $i) == 0)
  107. {
  108. return 0;
  109. }
  110. }
  111. return 1;
  112. }
  113. private static function mul_mod($a, $b, $m)
  114. {
  115. return fmod((double)$a * (double)$b, $m);
  116. }
  117. private static function inv_mod($x, $y)
  118. {
  119. $q = $u = $v = $a = $c = $t = 0;
  120. $u = $x;
  121. $v = $y;
  122. $c = 1;
  123. $a = 0;
  124. do
  125. {
  126. $q = (int)($v / $u);
  127. $t = $c;
  128. $c = $a - $q * $c;
  129. $a = $t;
  130. $t = $u;
  131. $u = $v - $q * $u;
  132. $v = $t;
  133. }
  134. while ($u != 0);
  135. $a = $a % $y;
  136. if ($a < 0)
  137. {
  138. $a = $y + $a;
  139. }
  140. return $a;
  141. }
  142. private static function pow_mod($a, $b, $m)
  143. {
  144. $r = $aa = 0;
  145. $r = 1;
  146. $aa = $a;
  147. while (1)
  148. {
  149. if ($b & 1)
  150. {
  151. $r = self::mul_mod($r, $aa, $m);
  152. }
  153. $b = $b >> 1;
  154. if ($b == 0)
  155. {
  156. break;
  157. }
  158. $aa = self::mul_mod($aa, $aa, $m);
  159. }
  160. return $r;
  161. }
  162. }
  163. ?>

人气教程排行