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

前端 未结 7 897
北恋
北恋 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 17:00

    The following version uses Ramanujan's formula as outlined in this SO post - it uses a relation between pi and the "monster group", as discussed in this article.

    import math
    
    def Pi(x):
        Pi = 0
        Add = 0
        for i in range(x):
            Add =(math.factorial(4*i) * (1103 + 26390*i))/(((math.factorial(i))**4)*(396**(4*i)))
            Pi = Pi + (((math.sqrt(8))/(9801))*Add)
        Pi = 1/Pi
        print(Pi)
    
    Pi(100)
    

提交回复
热议问题