Back in November of 2016, researchers managed to calculate pi to 22.4 trillion digits. This was done using something called the [Chudnovsky Algorithm](https://en.wikipedia.org/wiki/Chudnovsky_algorithm), which looks a little something like this:\n\n!(https://hackernoon.com/hn-images/1*tcEYR1URER7MSuRcy9PXTg.png)\n\nPrinting out all these digits at a reasonable font size would require a modest [139 miles of stacked paper](http://turner.faculty.swau.edu/mathematics/materialslibrary/pi/piprint.html).\n\nOf course, 22 trillion digits is about 22 trillion digits more than necessary for any application. [NASA only uses around 15](https://www.jpl.nasa.gov/edu/news/2016/3/16/how-many-decimals-of-pi-do-we-really-need/), and 40 can calculate the circumference of the known universe with a precision of the width of a hydrogen atom. Not bad.\n\nWhat’s good enough for NASA is good enough for me, so I decided to have my program find pi to the 15th decimal place (`3.141592653589793`).\n\nThe Chudnovsky Algorithm scares me with all of those factorials and exponents — I wanted something easier to program. I first turned to a nicer-looking (and much more inefficient) infinite series: the [Leibniz formula](https://en.wikipedia.org/wiki/Leibniz_formula_for_π). Here’s what it looks like:\n\n!(https://hackernoon.com/hn-images/1*GfRQ2oybmsjONSzZ8qYvUg.png)\n\nDoubles in Swift generally have a precision of 15 decimal places, which is perfect for my application. In a few minutes, I whipped up the following Swift program in Playgrounds:\n\nI left the program to run while I went to grab breakfast, thinking it wouldn’t take more than a few minutes. I came back to find my computer turning itself into a space heater while still working on the 6th decimal place. This was only around the 200,000th iteration of the while loop.\n\n> I should’ve heeded the warning that this is one of the more inefficient ways of computing digits of pi.\n\nIf someone reading this has access to a really powerful computer I’d love to see how far you can get before the whole system becomes sluggish.\n\nNext I turned to the slightly more efficient Nilakantha series:\n\n!(https://hackernoon.com/hn-images/1*dt_QKEvHCtVk9YJRJ89LRQ.png)\n\nAfter spending a few minutes getting Xcode respond (it was still working its way through Leibniz formula), I was eventually able to develop this:\n\nLearning from my last trial, this time I expected to wait for hours. Much to my surprise the program finished at 31958 iterations before I was done checking my code for errors. Not bad!\n\nClearly, Swift (and Swift Playgrounds) is not the best language to implement processor-intensive tasks. Lower-level languages such as C++ are better suited. However, I do find it interesting to see how implementations of these various formulas differ from system to system.\n\nA great resource for playing with the more efficient algorithms can be found [here](http://beej.us/blog/data/pi-chudnovsky-gmp/). As you can see, the Chudnovsky algorithm can get 14 digits in one iteration, even if there’s way more math involved per run.\n\nGive these Swift programs a try on your own system and tell me your results!\n\nTo see more of my writing, follow me and [check out my page on Medium](https://medium.com/@Alex_Wulff).