Leibniz formula for π - Is this any good? (Python)

前端 未结 7 933
北恋
北恋 2021-01-03 16:15

I\'m doing an exercise that asks for a function that approximates the value of pi using Leibniz\' formula. These are the explanations on Wikipedia:

7条回答
  •  半阙折子戏
    2021-01-03 16:52

    def myPi(iters):
        pi = 0
        sign = 1
        denominator = 1
    
        for i in range(iters):
            pi = pi + (sign/denominator)
            # alternating between negative and positive
            sign = sign * -1
            denominator = denominator + 2
    
        pi = pi * 4.0
        return pi
    
    pi_approx = myPi(10000)
    print(pi_approx)
    

提交回复
热议问题