Skip to content

Instantly share code, notes, and snippets.

Created April 16, 2015 05:46
Show Gist options
  • Save anonymous/fccb10ae76ba39dbcc63 to your computer and use it in GitHub Desktop.
Save anonymous/fccb10ae76ba39dbcc63 to your computer and use it in GitHub Desktop.
package main
import (
"fmt";
"math"
)
func Sqrt(x float64) float64 {
count := 0
var old_z, z float64 = 0, 1
for ; math.Abs(z-old_z) > .001; count++ {
old_z, z = z, z - (z*z - x) / 2*z
}
fmt.Printf("Ran %v iterations\n", count)
return z
}
func main() {
fmt.Println(math.Sqrt(2))
fmt.Println("Result: ", Sqrt(2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment