php 计算圆周率(π)的值的解决办法
内容摘要
这篇文章主要为大家详细介绍了php 计算圆周率(π)的值的简单示例,具有一定的参考价值,可以用来参考一下。
文章正文
这篇文章主要为大家详细介绍了php 计算圆周率(π)的值的简单示例,具有一定的参考价值,可以用来参考一下。
最后一行的200表示计算圆周率后200位。当然如果你觉得电脑足够强大,可以试试更高的位数。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | <code class = "language-php" ><?php set_time_limit(0); //没有这个代码可能会运行超时 class pi { public static function calc( $__N__ ) { $n = (int) $__N__ ; $av = $a = $vmax = $N = $num = $den = $k = $kq = $kq2 = $t = $v = $s = $i = 0; $sum = 0.0; $N = (int)(( $n + 20) * log(10) / log(2)); $sum = 0; for ( $a = 3; $a <= (2 * $N ); $a = self::next_prime( $a )) { $vmax = (int)(log(2 * $N ) / log( $a )); $av = 1; for ( $i = 0; $i < $vmax ; $i ++) { $av = ( $av * $a ); } $s = 0; $num = 1; $den = 1; $v = 0; $kq = 1; $kq2 = 1; for ( $k = 1; $k <= $N ; $k ++) { $t = $k ; if ( $kq >= $a ) { do { $t = (int)( $t / $a ); $v --; } while (( $t % $a ) == 0); $kq = 0; } $kq ++; $num = self::mul_mod( $num , $t , $av ); $t = (2 * $k -1); if ( $kq2 >= $a ) { if ( $kq2 == $a ) { do { $t = (int)( $t / $a ); $v ++; } while (( $t % $a ) == 0); } $kq2 -= $a ; } $den = self::mul_mod( $den , $t , $av ); $kq2 += 2; if ( $v > 0) { $t = self::inv_mod( $den , $av ); $t = self::mul_mod( $t , $num , $av ); $t = self::mul_mod( $t , $k , $av ); for ( $i = $v ; $i < $vmax ; $i ++) { $t = self::mul_mod( $t , $a , $av ); } $s += $t ; if ( $s >= $av ) { $s -= $av ; } } } $t = self::pow_mod(10, ( $n - 1), $av ); $s = self::mul_mod( $s , $t , $av ); $sum = (double) fmod ((double) $sum + (double) $s / (double) $av , 1.0); } return array ( 'n' => $n , 'v' => sprintf( '%09d' , (int)( $sum * 1e9)) ); } private static function next_prime( $n ) { do { $n ++; } while (!self::is_prime( $n )); return $n ; } private static function is_prime( $n ) { $r = $i = 0; if (( $n % 2) == 0) { return 0; } $r = (int)(sqrt( $n )); for ( $i = 3; $i <= $r ; $i += 2) { if (( $n % $i ) == 0) { return 0; } } return 1; } private static function mul_mod( $a , $b , $m ) { return fmod ((double) $a * (double) $b , $m ); } private static function inv_mod( $x , $y ) { $q = $u = $v = $a = $c = $t = 0; $u = $x ; $v = $y ; $c = 1; $a = 0; do { $q = (int)( $v / $u ); $t = $c ; $c = $a - $q * $c ; $a = $t ; $t = $u ; $u = $v - $q * $u ; $v = $t ; } while ( $u != 0); $a = $a % $y ; if ( $a < 0) { $a = $y + $a ; } return $a ; } private static function pow_mod( $a , $b , $m ) { $r = $aa = 0; $r = 1; $aa = $a ; while (1) { if ( $b & 1) { $r = self::mul_mod( $r , $aa , $m ); } $b = $b >> 1; if ( $b == 0) { break ; } $aa = self::mul_mod( $aa , $aa , $m ); } return $r ; } static function sub( $len ){ $lens = ceil ( $len / 10) + 2; $a = 1; $p = '3.' ; for ( $i =1; $i < $lens ; $i ++){ $v = self::calc( $a ); $p .= $v [ 'v' ]; $a = $a +9; } return $p ; } } echo pi::sub(200);</code> |
注:关于php 计算圆周率(π)的值的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释