In a February post, I wrote a few implementations of Borwein’s Pi-approximation algorithm, just for fun.

Today, I re-wrote it in C, and I can’t help but note that it’s:

- shorter than the other implementations
- equally clear, if not more clear

Something to think about, next time you want to put C down for not being modern and clean.

```
double borw(int iterations) {
double y = sqrt(2.0) - 1.0;
double a = 6.0 - 4.0*sqrt(2.0);
for(int k = 0; k < iterations; ++k)
{
double fy = pow(1 - pow(y,4),0.25);
y = (1.0 - fy)/(1.0 + fy);
a = a*pow(1.0+y,4.0) -
pow(2.0,2.0*k+3.0)*y*(1.0+y+y*y);
}
return 1.0/a;
}
```

Advertisements