Skip to content

Instantly share code, notes, and snippets.

@thenormalsquid
Created August 20, 2015 23:43
Show Gist options
  • Save thenormalsquid/95b5726a0d7ebab86223 to your computer and use it in GitHub Desktop.
Save thenormalsquid/95b5726a0d7ebab86223 to your computer and use it in GitHub Desktop.
Newton approx
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
delta, z := float64(1), float64(1)
for delta > 1e-9 {
prev := z
z = z - (math.Pow(z, 2) - x) / (2 * z)
delta = math.Abs(z - prev)
}
fmt.Println(math.Sqrt(x))
return z
}
func main() {
fmt.Println(Sqrt(4))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment