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

前端 未结 7 947
北恋
北恋 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条回答
  •  Happy的楠姐
    2021-01-03 17:06

    The Leibniz formula translates directly into Python with no muss or fuss:

    >>> steps = 1000000
    >>> sum((-1.0)**n / (2.0*n+1.0) for n in reversed(range(steps))) * 4
    3.1415916535897934
    

提交回复
热议问题