π is an important number. It is used to do calculations about circles and spheres, as well as to measure angles using radians. π has some interesting properties, such as being irrational. This means that it has infinitely many digits that d...
Using Nilakantha Series
and continues according to this pattern. So the algorithm that you want to write is as follows:
Create a new text file. You can use any IDE of your choice, or just a text editor. Give your file the extension .py so that your computer recognizes it as Python program file.
decimal
module. If you use Python without it or similar libraries, precision will be limited to 17 digits. This module, however, will allow you to have arbitrary precision for the digits. It is a default library of Python, so you do not need to install it separately.from decimal import *
getContext().prec = 100
def nilakantha(reps): # Calculations will be here return answer
Decimal
, because it is the number for which you want the high precision provided by the decimal
library. Also set a variable op to 1. That variable will be used later to alternate between addition and subtraction.def nilakantha(reps): answer = Decimal(3.0) op = 1 # Calculations will be here return answer
for
-loop. The for
-loop will set a variable n to 2 initially. Then it will do what written is inside the loop and increment the value of n by 2, and repeat this process until the upper limit — 2*reps+1 — is reached.def nilakantha(reps): answer = Decimal(3.0) op = 1 for n in range(2, 2*reps+1, 2): # Calculations will be here return answer
Decimal
, Python will convert the other parts accordingly. Program the formula, but also multiply it with op.for n in range(2, 2*reps+1, 2): result += 4/Decimal(n*(n+1)*(n+2)*op)
for n in range(2, 2*reps+1, 2): result += 4/Decimal(n*(n+1)*(n+2)*op) op *= -1
print("How many repetitions?") repetitions = int(input()) print(nilakantha(repetitions))
print("3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679")(If you need more digits of π for your comparison, you can copy them from the internet.)
from decimal import * getcontext().prec = 100 def nilakantha(reps): result = Decimal(3.0) op = 1 n = 2 for n in range(2, 2*reps+1, 2): result += 4/Decimal(n*(n+1)*(n+2)*op) op *= -1 return result print("How many repetitions?") repetitions = int(input()) print(nilakantha(repetitions)) print("3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679")
Using Monte Carlo Method
AquartercircleAsquare=14πr2r2=14π{displaystyle {frac {A_{quartercircle}}{A_{square}}}={frac {{frac {1}{4}}pi r^{2}}{r^{2}}}={frac {1}{4}}pi }
You can calculate π with:
4AquartercircleAsquare=π{displaystyle 4{frac {A_{quartercircle}}{A_{square}}}=pi }
random
has the function for generating random numbers. math
provides some mathematical functions, like the square root, which you will need for calculating the distance of a point. turtle
will draw what the program is doing. This will make it slower, but it can help understand the method and be interesting to watch for some time. If you want to calculate π fast, you should choose a different method anyway.import random import math import turtle
print("Insert number of points:") np = input() while not np.isdigit(): print("Insert number of points:") np = input() np = int(np)
turtle.speed("fastest")
length = 300 # radius of circle and length of the square in pixels
#draw y axis turtle.pensize(2) turtle.forward(length + 40) turtle.left(135) turtle.forward(20) turtle.back(20) turtle.left(90) turtle.forward(20) turtle.penup() turtle.home() turtle.pendown() #draw x axis turtle.left(90) turtle.forward(length + 40) turtle.left(135) turtle.forward(20) turtle.back(20) turtle.left(90) turtle.forward(20) turtle.penup() turtle.goto(0,length) turtle.left(45) turtle.left(180) turtle.pendown() #draw quarter of circle turtle.pencolor("red") turtle.circle(length,-90)
inside
) to 0.inside = 0 for i in range(0,np):
random.uniform()
function:#get dot position x = random.randint(0,length) y = random.randint(0,length)
d=(x2−x1)2+(y2−y1)2{displaystyle d={sqrt {(x_{2}-x_{1})^{2}+(y_{2}-y_{1})^{2}}}}
However, since the centre is located at (0,0), x1 and y1 are both 0 and can be ignored. The formula is simpler:
d=x22+y22{displaystyle d={sqrt {{x_{2}}^{2}+{y_{2}}^{2}}}}
In Python code (x2 and y2 are the coordinates that you got in the previous step):
#determine distance from center d = math.sqrt(x**2 + y**2)
if d length: inside += 1 turtle.pencolor("red") else: turtle.pencolor("blue")
#draw dot turtle.penup() turtle.goto(x,y) turtle.pendown() turtle.dot()
print("Inside of quarter-circle:") print(inside) print("Total amount of points:") print(np) print("Pi is approximately:") print((inside / np) * 4.0)
exitonclick()
function of the turtle
module. Otherwise, the window with the drawing would close when the calculations finish, and the user wouldn't have time to look at it. Add the line:turtle.exitonclick()
import random import math import turtle print("Insert number of points:") np = input() while not np.isdigit(): print("Insert number of points:") np = input() np = int(np) turtle.speed("fastest") length = 300 # radius of circle and length of the square in pixels #draw y axis turtle.pensize(2) turtle.forward(length + 40) turtle.left(135) turtle.forward(20) turtle.back(20) turtle.left(90) turtle.forward(20) turtle.penup() turtle.home() turtle.pendown() #draw x axis turtle.left(90) turtle.forward(length + 40) turtle.left(135) turtle.forward(20) turtle.back(20) turtle.left(90) turtle.forward(20) turtle.penup() turtle.goto(0,length) turtle.left(45) turtle.left(180) turtle.pendown() #draw quarter of circle turtle.pencolor("red") turtle.circle(length,-90) inside = 0 for i in range(0,np): #get dot position x = random.uniform(0,length) y = random.uniform(0,length) #determine distance from center d = math.sqrt(x**2 + y**2) if d length: inside += 1 turtle.pencolor("red") else: turtle.pencolor("blue") #draw dot turtle.penup() turtle.goto(x,y) turtle.pendown() turtle.dot() print("Inside of quarter-circle:") print(inside) print("Total amount of points:") print(np) print("Pi is approximately:") print((inside / np) * 4.0) turtle.exitonclick()
Update 02 June 2020
ncG1vNJzZmismaXArq3KnmWcp51ktbDDjK2mZq%2BinsGmecBmp7KsmKS7brzRqJ6rmZ1iwbB5wpqjnK2clsGmec%2Bi