Skip to content

Instantly share code, notes, and snippets.

@igaozp
Created January 14, 2018 05:42
Show Gist options
  • Select an option

  • Save igaozp/17677f623a505a2e9cd9d6168b6b1d71 to your computer and use it in GitHub Desktop.

Select an option

Save igaozp/17677f623a505a2e9cd9d6168b6b1d71 to your computer and use it in GitHub Desktop.
牛顿法计算平方根
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := 1.0
for i := 0; i < 1000; i++ {
z = z - ((z*z - x) / 2 * z)
}
return z
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(math.Sqrt(2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment