Almost π

Discussion thread for Almost π.

The series formula shows the sum variable running from 0 to n, but the code is only accepted if it runs from 0 to n-1.

1 Like

Hey @misho88 thanks for pointing that out!

The upper bound should actually be ∞ so I fixed that and made it clearer that the first n terms should be summed (and that the k=0 term counts as the first term). Not sure if it helps but the upper bound on the sum shouldn’t cause any more confusion.

1 Like

Hi, Why is my code getting a different result? It may be something dumb but I don’t get why:

def almost_pi(N):
    index = 0
    math4 = 0
    pi_value = 0
    while index <= N:
        math1 = ((-1) ** index)
        math2 = ((2*index)+1)
        math3 = math1/math2
        math4 = math4+math3
        pi_value = math4*4
        print("\n Indice ",index," | ",math1, "/", math2, " = ", math3, " siendo PI ", pi_value)
        index +=1
        
    return pi_value

print(almost_pi(25))

print("\n Tendría que ser 3.1815766854350325 \n")

It seems to aproximate pi too, though.

Ok I see, I was using the <= operator so I was running one more loop than intended.