2017-03-31 161 views
1

我正在編寫一個程序,該程序應該通過強力來確定數字的平方根。但是,某些數字(每次相同)。Python跳過數字

下面是代碼:

toCalc = 3 

guess = toCalc 
toCalc = round(toCalc, 2) 
while 1+1==2: 

     print "Trying ", guess, "..." 
     calc = guess * guess 
     calc = round(calc, 2) 
     print calc 
     guess = guess - 0.01 
     if calc == toCalc: 
       break 

這裏是輸出:

Trying 1.22 ... 
1.49 
Trying 1.21 ... 
1.46 
Trying 1.2 ... 
1.44 
Trying 1.19 ... 
1.42 
Trying 1.18 ... 
1.39 
Trying 1.17 ... 
1.37 
Trying 1.16 ... 
1.35 
Trying 1.15 ... 
1.32 
Trying 1.14 ... 
1.3 
Trying 1.13 ... 
1.28 
Trying 1.12 ... 
1.25 
Trying 1.11 ... 
1.23 
Trying 1.1 ... 
1.21 
Trying 1.09 ... 
1.19 
Trying 1.08 ... 
1.17 
Trying 1.07 ... 
1.14 
Trying 1.06 ... 
1.12 
Trying 1.05 ... 
1.1 
Trying 1.04 ... 
1.08 
Trying 1.03 ... 
1.06 
Trying 1.02 ... 
1.04 
Trying 1.01 ... 
1.02 
Trying 1.0 ... 
1.0 
Trying 0.99 ... 
0.98 
Trying 0.98 ... 
0.96 
Trying 0.97 ... 
0.94 
Trying 0.96 ... 
0.92 
Trying 0.95 ... 
0.9 
Trying 0.94 ... 
0.88 
Trying 0.93 ... 
0.86 
Trying 0.92 ... 
0.85 
Trying 0.91 ... 
0.83 

數量「嘗試」下是鈣和「嘗試」後的數字是猜測。

+0

爲什麼不使用math.sqrt –

+3

你的問題到底是什麼? –

+0

您的算法不起作用。嘗試找到一個更好的。 – Daniel

回答

0
>>> round(1.73 * 1.73, 2) 
2.99 
>>> round(1.732 * 1.732, 2) 
3.0 
>>> round(1.74 * 1.74, 2) 
3.03 

9 * 9 = 81,10 * 10 = 100並且我們不說81到100之間的數字被跳過。

您與1.73和1.74努力,他們都沒有能夠通過平方產生的3精確值。

這種行爲是「不跳躍的數字」,但它是所有關於精度。

浮點數不是那麼容易處理。對於這個特定的問題,使用0.001的差異解決了這個問題,但可能不適用於所有其他數字。

但是,下面是解決問題的代碼。

toCalc = 3 

guess = toCalc 
toCalc = round(toCalc, 2) 


while 1+1==2: 

    print "Trying ", guess, "..." 
    calc = guess * guess 
    calc = round(calc, 2) 
    print calc 
    guess = guess - 0.001 
    if calc == toCalc: 
      break 
1

當你把next guess這是old guess - 0.01,並且正方形,next square大約是old square - 0.02(使用一個binomic公式)。這意味着猜測平方列中的步驟大約爲0.02,因此數字缺失。

這是你的意思嗎?

更好的算法可能是使用平分(谷歌它)。

1

這是一個較短的選擇。它只需要5個步驟來計算的sqrt(3) 11個正確的小數,這要歸功於Newton's iteration

to_calc = 3 
guess = to_calc 
epsilon = 1e-13 

while abs(guess**2 > to_calc) > epsilon: 
    guess = 0.5*(guess + to_calc/guess) 

print(guess) 
# 1.73205080757 
print(guess**2) 
# 3.0 
0

我不能完全肯定你是問什麼,但下面的函數使用一個詳盡的近似解。它具有以下功能:

  • 開始用窮舉
  • 採取小步驟來產生猜測
  • 檢查,看看是否足夠接近小量的
def squareRootExhaustive(x, epsilon): 
"""Assumes x and epsilon are positive floats & epsilon < 1.""" 

    # x = number for which to calculate the square root 
    # epsilon = how close you want to get to the answer 

    increment = epsilon**2 # Number of steps it will take to find a good enough approximation 

    guess = 0.0 # start the answer at 0 

    # while the difference between guess^2 and the num 'x', for which 
    # we are trying to find the square root of, is greater or equals epsilon 
    # and guess * guess is less or equals x, increment the answer 
    while abs(guess**2 - x) >= epsilon and guess*guess <= x: 
     # print(ans)to check the number of exhaustive guesses it generates 
     # before returning the most accurate answer 
     guess += increment 
     if guess*guess > x: 
      raise ValueError 
    return "{} is the sqaure root of {}".format(round(guess, 2), x) 

squareRootExhaustive(3, 0.01) 

較低的值將給予更多的準確的答案,但會減慢計劃。較大的值將會提供更快的答案,但它們會不太準確。

該算法會產生複雜性問題。這就是爲什麼二分算法更好。