0
点赞
收藏
分享

微信扫一扫

求一个数的算数平方根

残北 2022-02-09 阅读 37
python

def average(a,b):
    return (a+b)/2
def squareRoot(num,low,high):
    for i in range(20):
        guess=average(low,high)
        if guess**2==num:
            print(guess)
        elif guess**2>num:
            high=guess
        else :
            low=guess
    print(num,"的算术平方根是",guess)
num=float(input())
i=0
while 1>0:
    if i**2==num :
        guess=i
        print(num,"的算术平方根是",guess)
        break
    elif i**2<num and (i+1)**2>num:
        low=i
        high=i+1
        print(low,high)
        squareRoot(num,low,high)
        break
    else:
        i=i+1

举报

相关推荐

0 条评论