<?php /** * pi_digits * * Calculate the digits of pi. * * @version 0.4 * @author Contributors at eXorithm * @link /algorithm/view/pi_digits Listing at eXorithm * @link /algorithm/history/pi_digits History at eXorithm * @license /home/show/license * * @param number $digits * @return mixed */ function pi_digits($digits=500) { $n = floor($digits * 14/4); $scale = 10000; $init = 2000; $carry = 0; $result = ''; for($i=0;$i<=$n;$i++) { $arr[$i] = $init; } for($i=$n;$i>0;$i=$i-14) { $sum = 0; for($j=$i;$j>0;$j--) { $sum = ($sum * $j) + ($scale * $arr[$j]); $arr[$j] = $sum % (($j*2)-1); $sum = floor($sum / (($j*2)-1)); } $result .= sprintf("%04d", ($carry + ($sum / $scale))); $carry = $sum % $scale; } if ($digits>1) { return $result[0].'.'.substr($result,1,$digits-1); } else { return substr($result,0,$digits); } } ?>