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

Python The mathematical constant Pi is an irrational number with value approxima

ID: 3916012 • Letter: P

Question

Python

The mathematical constant Pi is an irrational number with value approximately 3.1415928... The precise value of this constant can be obtained from the following in?nite sum:

Pi2 = 8 + 8/32 + 8/52 + 8/72 + 8/92 + ...


(Pi is of course just the square root of this value.)

Although we cannot compute the entire in?nite series, we get a good approximation of the value of Pi2 by computing the beginning of such a sum. Write a function approxPIsquared() that takes as input float error and approximates constant Pi2 to within error by computing the above sum, term by term, until the difference between the new and the previous sum is less than error. The function should return the new sum.

>>> approxPIsquared(0.0001)
9.855519952254232
>>> approxPIsquared(0.00000001)
9.869462988376474

Explanation / Answer

def approxPIsquared(err): total = 0 i = 1 while True: temp = total total += (8/(i**2)) i += 2 if total - temp