Pergunta
Write a function estimatePi()
to estimate and return the value of Pi based on the formula found by an Indian Mathematician Srinivasa Ramanujan. It should use a while loop to compute the terms of the summation until the last term is smaller than 1e-15. The formula for estimating Pi is given below:
As per Ramanujam's estimation
(I'm sorry, I'm unable to upload the image)
def estimatePi():
import math
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
k=0
final=0
pi=0
while pi<1e-15:
a=factorial(4*k)
b=(1103+26390*k)
c=factorial(k)
d=c**4
e=396**(4*k)
f=2*math.sqrt(2)/9801
final+=(a*b*f)/(d*e)
k+=1
pi=1/final
return pi
and, my problem is this: Expected answer was =3.14159265359 my answer is =3.14159273001
I was not able to find my fault :(. Can anyone help with this for me?
Solução
You answer is correct. Float numbers have problems with accuracy especially for large number of digits after the decimal point; and when calculating non-exact values.
You can see from your answer that it has estimated the value of pi correctly for 5 digits after the decimal.
Outras dicas
Couple of things are wrong with your code my friend. First of all, in your code remember that the variable pi is not in any shape or form equal to final. You are computing a while loop that doesn't iterate because pi is obviously a bigger number than 1e-15. So to put it simply, your code is just computing your formula at k=0 and it stops. So here is one possible way to do it:
def estimatePi():
import math
def factorial(n):
if n == 0:
return 1
else:
return math.factorial(n)
k=1 # we want it at k=1 not 0 since we already have k=0 as Final
Final=0.31830987844 #this is the value at k=0, the first value
while Final>1e-15: """ Note that 1e-15 is the limiting value and we want all b values that are less than 1e-15. k=2 is the final k value where b has the last smaller number than 1e-15."""
b=(2*math.sqrt(2)/9801)*(factorial(4*k)*(1103+26390*k))/((factorial(k)**4)*(396**(4*k)))
Final=Final+b
k=k+1
return 1/Final
print estimatePi()
#This gives you the number you are looking for ---3.14159265359.
Here is my code. It returns the result the same as required:
import math
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
def estimatePi():
f=2*math.sqrt(2)/9801
k=0
RHS = 0
while True:
num = factorial(4*k)*(1103+26390*k)
den = (factorial(k))**4 * 396**(4*k)
a = f*num/den
RHS += a
if a < 1e-15: break
k+=1
return 1/RHS
import math
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
def series_term(k):
a=factorial(4*k)
b=(1103+26390*k)
c=factorial(k)
d=c**4
e=396**(4*k)
return float(a*b)/(d*e)
def estimatePi():
k=0
final=0
while True:
term = series_term(k)
final += term
if term < 1.0e-15:
break
else:
k += 1
f=2*math.sqrt(2)/9801
pi = 1.0/(final * f)
return pi
The problem you're having is not with your code, but your understanding of the question being asked. The problem states:
It should use a while loop to compute the terms of the summation until the last term is smaller than 1e-15.
Make a variable equal to 1, change the condition of the while loop to read: while variable>=1e-15:
and in the while loop, set your variable equal to the LAST TERM of the SUMMATION. this should give you a more accurate value. For what it's worth, this generates the correct value, but pyschools
still did not pass my code.