Python跳过数字

时间:2017-03-31 12:28:53

标签: python python-2.7

我正在编写一个程序,该程序应该通过强力来确定数字的平方根。但是,某些数字(每次都相同)。

以下是代码:

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

"尝试"是计算和#34;尝试"之后的数字。猜猜。

4 个答案:

答案 0 :(得分:1)

当您使用next guess old guess - 0.01并将其平方时,next square大约为old square - 0.02(使用二元公式)。这意味着猜测平方列中的步长约为0.02,因此缺少数字。

这是你的意思吗?

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

答案 1 :(得分:1)

这是一个较短的选择。由于Newton's iteration,只需要5个步骤来计算sqrt(3)的11个正确小数:

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

答案 2 :(得分: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

答案 3 :(得分: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)

较低的epsilon值会给出更准确的答案,但会减慢程序的速度。较大的值会给出更快的答案,但它们的准确性会降低。

此算法会出现复杂性问题。这就是二分算法更好的原因。

相关问题