2

我正在尝试解决 Go Tour 的练习#8

我的解决方案失败并显示错误消息the process to take too long

怎么了 ?

package main

import (
    "fmt"
    "math"
)

func Sqrt(x float64) float64 {
    guess := 1.0
    i := 1
    for i < 10 {
        guess = guess - (math.Pow(guess, 2)-x)/(2*guess)
    }
    return guess
}

func main() {
    fmt.Println(Sqrt(2))
}
4

1 回答 1

7

你没有i在循环中增加变量,所以它总是< 10.

 //-----------v
for ; i < 10; i++ {
    guess = guess - (math.Pow(guess, 2)-x)/(2*guess)
}
于 2013-03-15T01:46:30.023 回答