# Using Python To Play With Pi(**π)**

## Analyzing the efficiency of the two most well-known Pi series with Matplotlib and PIL

*Hello! It’s a pleasure to have you here. Here’s what I’ll cover:*

- Presentation and compaction of two most well-known Pi series.
- Construction of an algorithm to measure the efficiency of these Pi series.
- Matplotlib for plot graphs of returned values from the algorithm.
- PIL for grouping the graphics into a gif.

*Observation: This is NOT a tutorial.*

Pi(π). Probably, when someone talks about it, a lot of formulas that you learned in high school come up to your mind as the circumference length or maybe the sphere volume. But here, I won’t be talking about it. My intention is to show the beauty and potential of code lines.

Just a little presentation: Pi is an irrational number with infinite decimal places considered by some mathematicians the most important number in the universe. This elegant and mysterious number just made the GPS possible and it’s present in almost all NASA operations.

I don’t intend to stay in Pi utilities. I want to focus on the different ways of defining it. Following are the most known ones:

- The ratio of the length of a circumference to its diameter.
- Leibniz Series
- Nilakantha Series

What I’m going to be doing here is contemplating and exploring those two series using the power that computers provide us. My goal is to build an algorithm that calculates and records the stopping point(position of the last added term of the series) along with the number of correct decimal places of pi.

**MATH PART**

Here’s what will be covered in this part:

Serie → Summation Expression → Algorithm

First Step: Pi Serie → Summation Expression

I could take* Leibniz Series* and *Nilakantha Series *to start soon building my algorithm. But, why don’t use math to shorten the way? It can be done just by compacting the formula in a summation expression. Like this:

Compacting **Leibniz Series:**

Compacting **Nilakantha Series:**

The typical elements will be the most essential part of our algorithm.

Second Step: Summation Expression → Algorithm

We’re not in the code yet. Let’s use a few fundamentals and logic of programming to turn the summation expression terms into loop structure terms. Here is it:

*That’s enough! Let’s code!*

# THE ALGORITHM CONSTRUCTION WITH PYTHON

First of all, we need to import the math and decimal libraries, both are native from Python. Math will be used to get the Pi value and Decimal to deal with the high-precision of decimal places.

To lighten a little, let’s build a method for the Nilakila Series that calculates up to the stopping point number one hundred and fifty thousand and records the accuracy of decimals places. When a new correct decimal place is “found”, the method will print the stopping point and the accuracy.

The output:

`On stopping point nº1, the accuracy is of 1 decimals places.`

On stopping point nº3, the accuracy is of 2 decimals places.

On stopping point nº8, the accuracy is of 3 decimals places.

On stopping point nº14, the accuracy is of 4 decimals places.

On stopping point nº33, the accuracy is of 5 decimals places.

On stopping point nº72, the accuracy is of 6 decimals places.

On stopping point nº168, the accuracy is of 7 decimals places.

On stopping point nº339, the accuracy is of 8 decimals places.

On stopping point nº752, the accuracy is of 9 decimals places.

On stopping point nº1406, the accuracy is of 10 decimals places.

On stopping point nº2944, the accuracy is of 11 decimals places.

On stopping point nº6806, the accuracy is of 12 decimals places.

On stopping point nº13894, the accuracy is of 13 decimals places.

On stopping point nº33299, the accuracy is of 14 decimals places.

On stopping point nº68697, the accuracy is of 15 decimals places.

On stopping point nº124716, the accuracy is of 16 decimals places.

On stopping point nº128120, the accuracy is of 17 decimals places.

On stopping point nº129968, the accuracy is of 18 decimals places.

On stopping point nº130312, the accuracy is of 19 decimals places.

On stopping point nº130346, the accuracy is of 20 decimals places.

On stopping point nº130348, the accuracy is of 21 decimals places.

Now, instead of printing, let’s reform the function so that it stores the values in two lists (stopping point list and decimal places accuracy list) for each series.

Applying the reformed function to the two series, we will have a total of four lists witch are: one stopping points list for Nilakila, one stopping points list for Leibniz, one decimal accuracy list for Nilakila, and one decimal accuracy list for Leibniz.

# VISUALIZATION WITH MATPLOTLIB AND PILLOW

Now, what we have to do is equalize the number of terms in the lists to provide better visualization in the graphs. After, plot 26 graphs with matplotlib, and grouping those graphs in a gif with PIL.