and continues according to this pattern. So the algorithm that you want to write is as follows:
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")
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")
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)
#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()