Skip to content

Instantly share code, notes, and snippets.

@zhjgithub
Last active January 24, 2018 13:16
Show Gist options
  • Save zhjgithub/ecc08050697ac643baadcf648f2e297a to your computer and use it in GitHub Desktop.
Save zhjgithub/ecc08050697ac643baadcf648f2e297a to your computer and use it in GitHub Desktop.
compute square root using formula z -= (z*z - x) / (2*z) called Newton's method
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
z := 1.0
for i := 0; i < 10; i++ {
z -= (z*z - x) / (2 * z)
}
return z
}
func main() {
fmt.Println(Sqrt(2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment