Skip to content

Instantly share code, notes, and snippets.

@alaztetik
Created June 8, 2022 11:40
Show Gist options
  • Save alaztetik/5f94e24533c22414a4e8c0423c3df9b8 to your computer and use it in GitHub Desktop.
Save alaztetik/5f94e24533c22414a4e8c0423c3df9b8 to your computer and use it in GitHub Desktop.
Newton–Raphson root finding method
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := float64(1)
s := float64(0)
for {
z -= (z*z - x) / (2*z)
if math.Abs(s-z) < 1e-15 {
break
}
s = z
}
return s
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(math.Sqrt(2))
}
// ref_1: A Tour of Go
// https://www.wikiwand.com/en/Newton%27s_method
// ref_2: Newton's method
// https://www.wikiwand.com/en/Newton%27s_method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment