One of the joys of Maths is the fact that the names for things sound impressive add to that the fact that the notation can look very off putting if you you don’t know what it says and this has the effect of making you seem very erudite when talking about it. With this in mind I thought I’d post a quick implementation of Borwein’s algorithm for convergance on Π.
The generation of increasing digits of Π is in itself completely pointless but makes an interesting area of research for mathematicians and computer scientists with parallel advances in algorithm and hardware. PHP is possibly the least suitable language to implement these computationally intense calculations. However, it is possible for todays PHP scripts to quickly generate digits that took hours of computing time in the 1950′s and 1960′s. Borwein’s algorithm is interesting for the increasing number of correct digits it gives with each iteration. According to wikipedia the algorithm is given by starting out with:

And then iterating

function borwein($precision){
// set scale with suitable spare room
bcscale($precision+5);
//inital conditions
$rootTwo = bcsqrt(2);
$a = bcsub(6, bcmul(4, $rootTwo));
$y = bcsub($rootTwo, 1);
//number of interations needed
$count = floor(log($precision)/log(4));
for($k = 0; $k < $count; $k++){
$sqrt = bcsqrt(bcsub(1,bcpow($y, 4)));
$right = bcsqrt($sqrt);
$top = bcsub(1, $right);
$bottom = bcadd(1, $right);
$y = bcdiv($top, $bottom);
$onePlusY = bcadd(1, $y);
$left = bcmul($a, bcpow($onePlusY, 4));
$y2 = bcpow($y, 2);
$power = bcpow(2, (2*$k+3));
$expansion = bcadd($onePlusY, $y2);
$right = bcmul($power, bcmul($y, $expansion));
$a = bcsub($left, $right);
}
return bcdiv(1, $a, $precision);
}
echo borwein(2037), PHP_EOL;

