Pi

Pi day

Today, Pi day.
I don't see the finding the point of finding more than 1 millionth digit of pi.
I don't like Pi get approximated... in decimal instead of binary.
I had Pie :)
My homework grade is asymptote to 0.
But life is good... I got invited to join the Suffolk team to participate in NYSML and later, possibility, ARML

Sometimes, happiness are so simple...
Maybe I even want to get a girlfriend sometime soon.
Life is gooooooooooooood.

I have to prepare like hell. This weekend, I have to study enough so I can get a 5 on AP calculus BC.

Why am I pushing myself so hard... maybe I'm a animal.. I see threat... and I can chose to fight or flight... I chose to fight..

New version of Pi calculator, break the speed bound!

You think use PHP to calculate the 2000 places after pi in 8 seconds is fast? you haven't see anything yet. This is the 3rd last attempt of finding pi (at least till 2020). I still have 2 more algorithms going to try. This one uses Borwein's quartic convergence algorithm on pi finding.

Borwein's quartic convergence algorithm in pi finding

You want to know the speed right? It uses less than 4 second to find the 2000th place of pi! and it is only slightly slower than the older pi calculator when precision is very low(between 1 to 30).

Feel free to check the source.
This was modified to be part of the BCext class I am working on right now.

function bcpi($precision){
	bcscale($precision+6);
	$a = bcsqrt(2);
	$b = 0;
	$p = bcadd(2, $a);
	$i = 0;
	$count = ceil(log($precision+6)/log(2))-1;
	while($i < $count){
		$sqrt_a = bcsqrt($a);
		$a1 = $a;
		$a = bcdiv(bcadd($sqrt_a,bcdiv(1,$sqrt_a)),2);
		$b_up = bcmul($sqrt_a,bcadd(1,$b));
		$b_down = bcadd($a1,$b);
		$b = bcdiv($b_up, $b_down);
		$p_up = bcmul(bcmul($p,$b),bcadd(1,$a));
		$p_down = bcadd(1, $b);
		$p = bcdiv($p_up, $p_down);
		++$i;
	}
 
return bcadd($p,0,$precision);
}

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);
}

PHP: Calculate PI

Note: Newer version of the pi calculation is here and a even more faster one here.
I want to release some Benchmark system can be uploaded to any web host and check how good the web hosting is. Pi (?) calculations spread widely as one of the most popular CPU benchmark system, that's why I have created a Pi calculator that can get to the accuracy you want. For now, there is still one thing I'm not sure yet, I will explain it after show you the code.

function bcpi($precision=30){
        $accuracy = ($precision+5)*45/32;
//accuracy worked till the 180th $precision, almost used 30 second to calculate
	bcscale($precision+5);
	$n = 1;
	$bcatan1 = 0;
	$bcatan2 = 0;
	while($n < $accuracy){
//atan functions
		$bcatan1 = bcadd($bcatan1, bcmul(bcdiv(pow(-1, $n+1), $n * 2 - 1), bcpow(0.2, $n * 2 -1)));
		$bcatan2 = bcadd($bcatan2, bcmul(bcdiv(pow(-1, $n+1), $n * 2 - 1), bcpow(bcdiv(1,239), $n * 2 -1)));
		++$n;
	}
	return	bcmul(4,bcsub(bcmul(4, $bcatan1),$bcatan2),5);
}

This Pi formula is not the fastest(converge speed), but it is one of the easiest. it uses Machin's formula + Taylor Series work together.

So, this is the BCMath version of atan() and pi() .
Why not use the original pi()? because from the statement of Keamos at gmail on 13-Jan-2006 09:31, it shows pi() have a precision limit. Note this script is very slow, and the time it need is increase squarely, so 100 precision could use up to 1 second.

Now, I have only one problem, the Pi's accuracy, Pi's accuracy are not the same as it's precision. Precision is the number of decimal it returns, the accuracy is the closeness to the real pi's value rounded the same precision. I want to output any precision with 100% accuracy, then I must find the relationship between two of them. You guessed it right, I don't know :aok:
Edit: it looks like precision:accuracy is somewhere around 45:32.
Just Added on 2007-01-20: cut off the last few numbers because it usually not 100% accurate
If anything know something about that, please tell me about it. Thanks in advance.

Syndicate content
Honey Pot that kill bots