Skip to content

Instantly share code, notes, and snippets.

@danielpcox
Created April 18, 2016 21:21
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 danielpcox/9a6be136fe5c4136137f3658f7985ba5 to your computer and use it in GitHub Desktop.
Save danielpcox/9a6be136fe5c4136137f3658f7985ba5 to your computer and use it in GitHub Desktop.
Newton's method, solving https://tour.golang.org/flowcontrol/8
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := 1.0
l := 0.0
for math.Abs(z-l)>9e-15 {
l = z
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