Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The mathematical constant pi can be approximated to at least 11 decimals by usin

ID: 3851132 • Letter: T

Question

The mathematical constant pi can be approximated to at least 11 decimals by using the mathematical algorithm: pi = squareroot 12 (1/1 middot 3^- 1/3 middot 3^+ 1/5 middot 3^- 1/7 middot 3^+ 1/9 middot 3^..) Notice that the sign alternates from minus (-)to plus (+) for each term. This approximation becomes more accurate as you increase the number of terms used. You need to evaluate this calculation based on a varying number of terms, (i.e.: how accurate is the approximation with 50, 100, 500, 1000 or more terms)

Explanation / Answer

import math
from decimal import Decimal
pie = Decimal(float(22.0/7))
def oddgenerator(n):
odd = []
for i in range(n):
if i%2==0:
pass
else:
odd.append(i)
return odd
  
def Pie(n=100):
start = Decimal(math.sqrt(12))
odd = oddgenerator(n)
sum = 0
for i in range(len(odd)):
if i%2==0:
sum += 1/Decimal(odd[i]*(3**i))
else:
sum -= 1/Decimal(odd[i]*(3**i))
total = start * sum
return Decimal(total)
print "This program approximate value of PI"
print "More Elements increase accuracy"
print "This program support any limit of number"
n=int(raw_input("Enter number of elements: "))
print "Number of Elements"+str(n);
p=Pie(n)
print "Approx PI: "+str(p)
print "Correct PI: "+str(pie)
print "Difference :"+ str(Decimal(pie-p))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote