PHP: Calculate Pi, revisited

Note: Even Faster pi calculation is here
The Pi calculation system I released previously was very slow, 180 digits could cost about 30 seconds. That's why I have to release a much faster system.
Especially after I find out that Yonatan Naamad have produced a faster script that calculate 200 digits of pi in 10 seconds.
So here is the newest release of bcpi, it generated 1000 digits of pi in 5 seconds! Should be the fastest one alive. Why is it fast? well it follows Chudnovsky brothers's Chudnovsky algorithm.
The Chudnovsky algorithm
Each time k plus one, 14 of pi's digit will be find, so this is much more efficient than the old one.
The new function:

function bcfact($n){
  return ($n == 0 || $n== 1) ? 1 : bcmul($n,bcfact($n-1));
}
function bcpi($precision){
	$num = 0;$k = 0;
	bcscale($precision+3);
$limit = ($precision+3)/14;
while($k < $limit){
	$num = bcadd($num, bcdiv(bcmul(bcadd('13591409',bcmul('545140134', $k)),bcmul(bcpow(-1, $k), bcfact(6*$k))),bcmul(bcmul(bcpow('640320',3*$k+1),bcsqrt('640320')), bcmul(bcfact(3*$k), bcpow(bcfact($k),3)))));
	++$k;
}
return bcdiv(1,(bcmul(12,($num))),$precision);
}

Comments

Anonymous's picture

[...] PHP: Calculate Pi,

[...] PHP: Calculate Pi, revisited [...]

Anonymous's picture

[...] PHP: Calculate Pi,

[...] PHP: Calculate Pi, revisited [...]

Post new comment

  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <span> <fn>
  • Lines and paragraphs break automatically.
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].
  • Mathematical equations and graphs can be added between [tex] and [/tex], [graph] and [/graph] tags.
  • Textual smileys will be replaced with graphical ones.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Honey Pot that kill bots