Calculate Digits of Pi Using Python

by Prostyle4444 in Circuits > Software

100 Views, 2 Favorites, 0 Comments

Calculate Digits of Pi Using Python

Screenshot 2025-03-10 222748.png

In this project, you can calculate "N" digits of pi, which is related to the number of iterations you run on the program. I'll get more into it later in the project. We do this by calculating the Maclaurin series of the inverse trignometric function of the Sine function at 0.5 as ArcSin(0.5) = Pi/6

Or,

6 * Arcsin(0.5) = Pi

We will be calculating the infinite series of Arcsin(x) = Summation 0-infinity{(2n-1)!!*(x^(2n+1)}/{(2n)!!*(2n+1)}

[The derivation is a bit long, I will share a link of my question on math StackOverflow on which I have based the project for you math lovers -

https://math.stackexchange.com/questions/4140154/help-me-prove-the-summation-function-for-arcsin-x

The above link gives an idea of how the formula was derived

]

For ArcSin(0.5) = Summation 0-infinity{(2n-1)!!*(0.5^(2n+1)}/{(2n)!!*(2n+1)}

Now, since there is a limitation on how many floating places python can process, we will be using big Integer which is included by default in Python.

Supplies

Laptop with preferably Windows 10 or 11

A Python IDE

Screenshot 2025-03-10 224329.png

We first import the Inbuilt Python Math Library. Then, we declare the necessary variables (we will see why they are necessary in the next steps)

Screenshot 2025-03-10 224549.png

This is a quick workaround to find the double factorial using the multiplication function (mul) of the Math Library. We will be using this function in our next step.

Screenshot 2025-03-10 224853.png

This is the main function used to calculate Pi. We calculate the values of (2n-1)!! and (2n)!! separately and we store them in separate variables.

Now finally we do the calculation for every iteration of n. Note that this formula has more steps than the above-mentioned formula as we will only be using integers as integers in Python do not have a size limitation like Floating Point Units in Python. We use integer division (denoted by a double slash "//"). We also use an exponential of 10 in the above program (10**itr) to ensure the digits stay Integer. We also use 0.5 = 1/2 and multiply 2 in the denominator (2**(2x+1)). You can comment below if you have any doubts about the implementation.

Saving the Digits of Pi

Screenshot 2025-03-10 230049.png

Roughly the number of accurate digits generated are ~ 1/2 of the iteration. So we will only save half of the numbers generated into a txt file called pi.txt. I have added a simple routine to slice the string to half its size above. We save the digits in a txt file called "pi.txt"

I have attached the complete Python program for your reference.I have also attached 5000 values of Pi generated by the program (Took roughly about 5 minutes on my laptop).

If you want to generate and save more than 4300 digits in Python, you would have to specific in a system routine. You can do so by importing the system library (import sys) and entering sys.set_int_max_str_digits(length_pi) where length_pi is the length of the number of pi digits you want to generate.


Please remember I have set the number of Pi digits saved is half of the digits that are generated.


So, if you want to generate 1000 digits of Pi, you would need to set the iterations to 2000-2200 when prompted. Please comment if you have any questions! Happy Coding!! And a very happy Pi Day in Advance.

Downloads