欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

使用BBP公式计算Pi的代码

程序员文章站 2024-01-07 12:28:10
...
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 {
  18. $vmax = (int)(log(2 * $N) / log($a));
  19. $av = 1;
  20. for ($i = 0; $i {
  21. $av = ($av * $a);
  22. }
  23. $s = 0;
  24. $num = 1;
  25. $den = 1;
  26. $v = 0;
  27. $kq = 1;
  28. $kq2 = 1;
  29. for ($k = 1; $k {
  30. $t = $k;
  31. if ($kq >= $a)
  32. {
  33. do
  34. {
  35. $t = (int)($t / $a);
  36. $v --;
  37. }
  38. while (($t % $a) == 0);
  39. $kq = 0;
  40. }
  41. $kq ++;
  42. $num = self::mul_mod($num, $t, $av);
  43. $t = (2 * $k -1);
  44. if ($kq2 >= $a)
  45. {
  46. if ($kq2 == $a)
  47. {
  48. do
  49. {
  50. $t = (int)($t / $a);
  51. $v ++;
  52. }
  53. while (($t % $a) == 0);
  54. }
  55. $kq2 -= $a;
  56. }
  57. $den = self::mul_mod($den, $t, $av);
  58. $kq2 += 2;
  59. if ($v > 0)
  60. {
  61. $t = self::inv_mod($den, $av);
  62. $t = self::mul_mod($t, $num, $av);
  63. $t = self::mul_mod($t, $k, $av);
  64. for ($i = $v; $i {
  65. $t = self::mul_mod($t, $a, $av);
  66. }
  67. $s += $t;
  68. if ($s >= $av)
  69. {
  70. $s -= $av;
  71. }
  72. }
  73. }
  74. $t = self::pow_mod(10, ($n - 1), $av);
  75. $s = self::mul_mod($s, $t, $av);
  76. $sum = (double)fmod((double)$sum + (double)$s / (double)$av, 1.0);
  77. }
  78. return array(
  79. 'n' => $n,
  80. 'v' => sprintf('%09d', (int)($sum * 1e9))
  81. );
  82. }
  83. private static function next_prime($n)
  84. {
  85. do
  86. {
  87. $n ++;
  88. }
  89. while (!self::is_prime($n));
  90. return $n;
  91. }
  92. private static function is_prime($n)
  93. {
  94. $r = $i = 0;
  95. if (($n % 2) == 0)
  96. {
  97. return 0;
  98. }
  99. $r = (int)(sqrt($n));
  100. for ($i = 3; $i {
  101. if (($n % $i) == 0)
  102. {
  103. return 0;
  104. }
  105. }
  106. return 1;
  107. }
  108. private static function mul_mod($a, $b, $m)
  109. {
  110. return fmod((double)$a * (double)$b, $m);
  111. }
  112. private static function inv_mod($x, $y)
  113. {
  114. $q = $u = $v = $a = $c = $t = 0;
  115. $u = $x;
  116. $v = $y;
  117. $c = 1;
  118. $a = 0;
  119. do
  120. {
  121. $q = (int)($v / $u);
  122. $t = $c;
  123. $c = $a - $q * $c;
  124. $a = $t;
  125. $t = $u;
  126. $u = $v - $q * $u;
  127. $v = $t;
  128. }
  129. while ($u != 0);
  130. $a = $a % $y;
  131. if ($a {
  132. $a = $y + $a;
  133. }
  134. return $a;
  135. }
  136. private static function pow_mod($a, $b, $m)
  137. {
  138. $r = $aa = 0;
  139. $r = 1;
  140. $aa = $a;
  141. while (1)
  142. {
  143. if ($b & 1)
  144. {
  145. $r = self::mul_mod($r, $aa, $m);
  146. }
  147. $b = $b >> 1;
  148. if ($b == 0)
  149. {
  150. break;
  151. }
  152. $aa = self::mul_mod($aa, $aa, $m);
  153. }
  154. return $r;
  155. }
  156. }
  157. ?>
复制代码