Hacker News new | ask | show | jobs
by LeoPanthera 2474 days ago
On any standard unix system with bc installed - it's preinstalled on most of them, you can calculate pi to $n digits using bc:

bc -l <<< "scale=$n; 4*a(1)"

2 comments

Yes, 4 * arctan(1) will do in any language. I just find some joy in knowing a formula that uses only addition, multiplication, and addition, and computing it directly.
The algorithm seems to be at least quadratic in the length. On a 2014 i7 Mac mini, (n, time(sec)) = (1000, 0.29), (2000, 1.65), (4000, 9.70), (8000, 58.42).
I think this section on Wikipedia is relevant: https://en.wikipedia.org/wiki/Approximations_of_%CF%80#Grego...