当前位置:天才代写 > tutorial > Python教程 > python newton method 求解平方根

python newton method 求解平方根

2018-05-17 08:00 星期四 所属: Python教程 浏览:303

python">#!/usr/bin/python

def sqrt_iter(guess,x):
    if(good_enough(guess, x)):
        print guess
    else:
        sqrt_iter(improve(guess, x),x)

def improve(guess, x):
    return average(guess, x/guess)

def average(x,y):
     return (x+y)/2

def good_enough(guess,x):
    if(abs(guess * guess -x) < 0.0001):
        return True
    else:
        return False

def sqrt_oliver(x):
    sqrt_iter(1.0,x)

sqrt_oliver(5)

标签:python

 

    关键字:

天才代写-代写联系方式