Skip to content

Instantly share code, notes, and snippets.

@pyk
Last active August 29, 2015 13:57
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 pyk/9859076 to your computer and use it in GitHub Desktop.
Save pyk/9859076 to your computer and use it in GitHub Desktop.
my solution on A Tour of Go Exercise: Loops and Functions , http://tour.golang.org/#24
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := x
n := 0.0
for math.Abs(n-z) > 1e-10 {
n, 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