These are three simple functions written in python which uses the Nilakantha Series ( which was developed in the 15th century. ),Monte Carlo method and the Gregory-Leibniz series.
The Nilakantha Series is the formula below it works out an approximation of pi depending on the amount of times it recurs on for using the equation below.
The Gregory-Leibniz Series is very similar to the Nilakantha Series with just using a different equation
However the Monte Carlo method involves creating a square with an inner circle of the same dimensions and plotting random points. Then dividing the points in the circle by the total points and multiplying by 4.
Nilakantha Series
def piCalc(recur):
pi, den, operator = 3, 2, 1
for i in range(0, recur):
pi = (pi) + (operator * 4 / ((den) * (den + 1) * (den + 2)))
if operator == 1:
operator = -1
else:
operator = +1
den = den + 2
return(pi)
Monte Carlo methord
def piCalc2(total):
count = 0
for i in range(total):
x = random()
y = random()
if x*x + y*y <= 1:
count = count + 1
return str(4 * count / total)
Gregory-Leibniz series
def piCalc3(recur):
pi, den, operator = 0, 1, 1
for i in range(0, recur):
pi = (pi) + (operator * 4 / (den))
if operator == 1:
operator = -1
else:
operator = +1
den = den + 2
return pi
Simply call the functions with a specified amount to recur, higher the amount higher the precision. For the Monte Carlo methord you need to import random but the others dont require anything
print("Pi using Nilakantha Series:", piCalc(500000))
print("Pi using Monte Carlo Method:", piCalc2(500000))
print("Pi using Gregory-Leibniz series:", piCalc3(500000))
I might of got some of the terminology wrong so dont quote me on anything.