2017-03-31 163 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) 

较低的值将给予更多的准确的答案,但会减慢计划。较大的值将会提供更快的答案,但它们会不太准确。

该算法会产生复杂性问题。这就是为什么二分算法更好。