Skip to content

Instantly share code, notes, and snippets.

@xiaokangwang
Created March 8, 2015 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaokangwang/e0e63c09ecaf3d638175 to your computer and use it in GitHub Desktop.
Save xiaokangwang/e0e63c09ecaf3d638175 to your computer and use it in GitHub Desktop.
Newton's method for sqrt()
package main
import "fmt"
func main() {
i := 1
var sqr, x, cm float64
sqr = 16
x = 1
for i != 0 {
cm = ((x*x - sqr) / (x * sqr))
x = x - cm
if cm < 1e-7 && -cm < 1e-7 {
i = 0
}
}
fmt.Println(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment