Does anyone know how to fix it? In this Programming Assignment you will write a
ID: 3689021 • Letter: D
Question
Does anyone know how to fix it?
In this Programming Assignment you will write a function named LeibnizPi (n) which takes an integer argument n and computes the approximate value of and prints it. The accuracy of the computed will depend on the value of the integer n which is the number of terms in the Leibniz formula for . If we take infinitely many terms, we can compute exactly. Here is the Leibniz sum formula: 0O 4 2i+11 3 579 11 Since taking infinitely many terms is outside our ability, we can take finitely many terms, which is determined by n, and compute as accurately as needed by selecting a large value of n (-1)n-1 4 For example, if we take n=4, we will have 4 terms and thus compute as 4. 2i+11 3 57 02 02 4.76 105 2.8952380952380956 which is a very bad approximation. If we take 10 terms, this summation becomes 3.0418396189294032 which is closer to commonly used value of =3.14 but still not as good as it needs to be. If you need better approximations, you will need to take much larger values of nExplanation / Answer
def LeibnizPi(n):
res=0.0;
for i in range(1,n+1):
if(i%2==0):
res=res-(1/(2.0*i-1));
else:
res=res+(1/(2.0*i-1));
print 4*res
n=int(raw_input());
LeibnizPi(n);
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.