Finding the square root using Newton's method (errors!)

前端 未结 5 1524
执念已碎
执念已碎 2020-12-11 10:06

I\'m working to finish a math problem that approximates the square root of a number using Newton\'s guess and check method in Python. The user is supposed to enter a number,

5条回答
  •  感动是毒
    2020-12-11 10:48

    All you need to know is the nth term in the sequence. From the Leibniz series, we know this is ((-1)**n)/((2*n)+1). Just sum this series for all i with an initial condition of zero and you're set.

    def myPi(n):
    
    pi=0
    for i in range(0,n):
        pi=pi+((-1)**i)/((2*i)+1)
    return 4*pi
    print (myPi(10000))
    

提交回复
热议问题