Introduction: Using Pi-thon to Calculate Pi !
Hi Everybody,
In this Instructable, I will teach how to use python to calculate π and the logic behind the program. I will also be discussing various ways on how to calculate π using math and how we are going to implement it into the code itself.
Bonus! : I will also be teaching how to calculate π using BlueJ too!
Pls keep an open mind as there is a lot of math and coding involved but I will teach it in such a way that it is interesting for everyone.
I am submitting this project to the Instructables Pi Day Speed Challenge. Please do vote for me by clicking on this hyperlink. :)
Supplies
- PC / Laptop
- Python / BlueJ
Step 1: What Is Pi?
Before we start with the programming let us start with the explanation of π.
π is the ratio of the circle's circumference to its diameter. The circumference is the perimeter of a circle and the diameter is the length of the line through the center that touches two points on the edge of the circle. You can see them in the picture above. Take the ratio between them and you can calculate π.
You might ask yourself, why can't I take my ruler and measure it myself; This cant be done because rulers won't give you the most precise measurement of the circle and your value of π will be completely off.
Why do we use π?
π is a never-ending number. π is used in math to calculate the areas and volumes of various objects. It is also used widely in trigonometry. π appears in anything with a circle. It is one of the most important numbers in life.
Since we stated before that it is not possible to just take out your ruler and just calculate π we will look at some math formulas on how to calculate π.
Credit to Picture goes to https://amazingarchimedes.weebly.com/real-life-application-of-pi.html
Step 2: Calculating Pi Using Math
There are two ways to calculate π using math. They are calculated using the:
- Gregory-Leibniz series
Nilakantha series
Mathematicians have found several different mathematical series that, if carried out infinitely, will accurately calculate π to a great number of decimal places. Some of these are so complex they require supercomputers to process them. But there are more simple methods like the ones above.
Step 3: Gregory-Leibniz Series
Gregory-Leibniz series is an infinite series that uses the basic formula of π = 4(1 - 1/3 + 1/5 - 1/7 + 1/9 and so on. We can further simplify it by writing it as π = (4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 - 4/15 ... which is just
π = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 - 4/15 ...
You might be wondering if it will forever go on, how will we calculate it, well this is why we are using python to calculate it. We can create a program with a loop with the denominator changing to the next odd number and at the same time changing the positive-negative sign.
Now we will move onto the second way to calculate π.
Step 4: Nilakantha Series
The Nilakantha Series is another infinite series to calculate pi that is fairly easy to understand. While it is somewhat more complicated than the latter, it converges on pi much quicker than the Gregory-Leibniz series.
For this formula, take three and start alternating between adding and subtracting fractions with numerators of 4 and denominators that are the product of three consecutive integers which increase with every new iteration. Each subsequent fraction begins its set of integers with the highest one used in the previous fraction.
We can write it mathematically like this:
π = 3 + 4/(2*3*4) - 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + 4/(10*11*12) - 4/(12*13*14) ................
We can create a program with a loop using the formula above.
Step 5: Python Code for Calculating Pi
Logic:
We are going to use Gregory-Leibniz’s formula to calculate π using python as it is easier to code.
Approach:
1) Initialize k=1 // this variable will be used as the denominator of Leibniz's formula, it will be incremented by 2
2) Initialize sum=0 // sum will add all the elements of the series
3) Run a for loop from 0 to 1000000 //at this value, we get the most accurate value of Pi
4) Inside for loop, check if i%2==0 then do sum=sum+4/k
5) Else, do sum=sum-4/k
6) Increment k by 2, go to step 3
Implementation:
# Initilize denominator
k = 1
# Initilize sum
s = 0
for i in range(1000000):
# even index elements are positive
if (i % 2) == 0:
s + = 4/k
else:
# odd index elements are negative
s -= 4/k
# denominator is odd
k += 2
print(s)
You can copy and paste the code into python.
You will get the following output: 3.1415926535
Step 6: BlueJ Code for Calculating Pi
This code will also use the same logic as the python code. BlueJ programming and Python coding are kinda different so this might look weirder than the last. But it will still give an accurate reading of π.
Code:
public class pi { //initialising class
public static void main(String[] args) {
double pi=0;
double s =1; //numerator will always be one, numerqator will later be multiplied by 4 at the end
double n = 1000000; //number of times the loop will run so that it will give an accurate result
for(int i=1; i
pi +=s/1: //increasing denominator
s=-s
}
System.out.println("Pi =" +pi/4); // Dividing it with by 4 here so the formula gets multiplied by 4
}
}
Step 7: Pie
Thank you for reading this Instructable. I hope you learnt more about how to calculate π. π is a very interesting number, and I suggest all of you read more about it. Please try using my codes on your own and try to figure out the logic of the programs.
Fun Tip: You can remember the first 20 digits of pi by using this pneumonic device : "I wish I could remember pi, "Eureka!" cried the great inventor, Christmas pudding, Christmas pie, Is the problem's very center ". Count the number of letters in each word and you shall get the value of pi.
I am submitting this project to the Instructables Pi Day Speed Challenge. Please do vote for me by clicking on this hyperlink. :)
Check out my YouTube videos: https://www.youtube.com/channel/UCcu0AbsG0LjzM5cm...
Check out my Instagram Account: https://www.youtube.com/channel/UCcu0AbsG0LjzM5cm...
Check out my other Instructables: https://www.youtube.com/channel/UCcu0AbsG0LjzM5cm...
Thank You!