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

前端 未结 7 894
北恋
北恋 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:16

    old thread, but i wanted to stuff around with this and coincidentally i came up with pretty much the same as user3220980

    # gregory-leibnitz
    # pi acurate to 8 dp in around 80 sec
    # pi to 5 dp in .06 seconds
    import time
    start_time = time.time()
    
    pi = 4 # start at 4
    times = 100000000
    
    for i in range(3,times,4):
            pi -= (4/i) + (4/(i + 2))
        
    print(pi)
    print("{} seconds".format(time.time() - start_time))
    
    0 讨论(0)
提交回复
热议问题