I was attempting an approximated value of pi through the formula of
pi= 3 + (4/(2*3*4)) - (4/(4*5*6)) + (4/(6*7*8)) - …
(and so on). However, my code (shown below) had 2 separate answers (3.1415926535900383 and 3.141592653590042) when:
- approx variable started with "0" and "3" respectively
- n=10000
Does anyone know why?
def approximate_pi(n):
approx=0
deno=2
if n == 1:
return 3
for x in range(n-1):
if x%2:
approx -= 4/((deno)*(deno+1)*(deno+2))
else:
approx += 4/((deno)*(deno+1)*(deno+2))
deno+=2
return approx+3
and
def approximate_pi(n):
approx=3
deno=2
if n == 1:
return 3
for x in range(n-1):
if x%2:
approx -= 4/((deno)*(deno+1)*(deno+2))
else:
approx += 4/((deno)*(deno+1)*(deno+2))
deno+=2
return approx