Hey all, beginner here. (Background:I have a BS in Chemical Engineering and an MS in Industrial Engineering but have always wanted to learn Python!!)
I was trying a program to find the value of pi. I wrote a code for it and it works well, except it crashes at n > 994.
I used recursion here.
Can anyone let me know why it crashes after n=994 :/
Here’s my code.
#Program that prints the approximate value of pi
#4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11...
#nth term of the sequence is: \n", (4/(2*n-1)*(-1)**(1-n))) using simple math.
#Code begins
import math
def T(n):
Tn =(4/(2*n-1)*(-1)**(1-n)) #using some simple math for arithmetic progressions
if n==0:
return 0
if n==1:
return 4
else:
return Tn+T(n-1)
n=eval(input("Enter the value of n: \n"))
print ("The approximate value of pi is:", T(n))
print ("The difference from the actual value of pi is:", T(n)-math.pi) “
#Code ends