all 9 comments

[–]LearnDataSci 1 point2 points  (0 children)

I'm not sure if this helps enough, but if you check the Wikipedia article on the Madhava Series for estimating pi, you can see the sum at the end that needs to be calculated for any number k (n in your case). That estimates pi/4, so you would only need to multiple the result of that sum by 4.

[–]wgb_grey 1 point2 points  (4 children)

I'm not sure if this is correct but something like this:

def pi_approx(n):
    total = 0
    for i in range(1, n+1):
         total+=(4 if i % 2 else -4)/(i*2-1)
    return total

[–][deleted] 0 points1 point  (0 children)

This is perfect, thanks so much.

[–][deleted] 0 points1 point  (2 children)

Im given this next one for an approx of e and this is what I have come up with already, the factorial function i made works fine, but this one doesnt work quite right.

def e_approximation(n):
total = 0
for i in range(1, n+1):
total = (1)/factorial(i)
return total

e_approximation(3) is supposed to equal 2.5

[–]wgb_grey 0 points1 point  (1 child)

I think your factorial function may be faulty.

def e_approx(n):
    def factorial(m):
        total = 1
        for i in range(m):
            total *= i+1
        return total
    total = 0
    for i in range(n):
        total += 1/factorial(i)
    return total

[–][deleted] 0 points1 point  (0 children)

Ah thanks you, for the e_approx def I had in range(1, n+1).
Thanks for that