Answers for "approximate pi"

1

approximate pi

# Approximation of the number PI through the Nilakantha's series

from decimal import *
getcontext().prec = 100

s = Decimal(1);   #Signal for the next operation
pi = Decimal(3);

n = input("Enter the number of iterations: ")         

for i in range (2, n * 2, 2):
    pi += (s*(Decimal(4)/(Decimal(i)*(Decimal(i)+Decimal(1))*(Decimal(i)+Decimal(2)))))
    s *= -1

print ("Aproximated value of PI :")
print (pi)
Posted by: Guest on October-25-2021

Python Answers by Framework

Browse Popular Code Answers by Language