Skip to content

Instantly share code, notes, and snippets.

@shalomb
Last active November 16, 2019 21:23
Show Gist options
  • Save shalomb/c00c03a76835d7c3768e8d58dade552c to your computer and use it in GitHub Desktop.
Save shalomb/c00c03a76835d7c3768e8d58dade552c to your computer and use it in GitHub Desktop.
// Square root implementation using Newton's Method
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) (z float64) {
z = x
_z := z
for {
z -= (z*z-x)/(2*z)
if math.Abs(_z-z) == 0 { break }
_z = z;
}
return
}
func main() {
fmt.Printf("\n%v\n%v", Sqrt(500), math.Sqrt(500))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment