Skip to content

Instantly share code, notes, and snippets.

@n3dst4
Created December 6, 2013 14:13
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 n3dst4/7824603 to your computer and use it in GitHub Desktop.
Save n3dst4/7824603 to your computer and use it in GitHub Desktop.
Sample solution for http://tour.golang.org/#24
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) (float64, int) {
last := 0.0
z := 1.0
i := 0
for math.Abs(last - z) > 0.0000001 {
i++
last = z
z = z - ((math.Pow(z, 2) - x) / (z * 2))
}
return z, i
}
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