Skip to content

Instantly share code, notes, and snippets.

@wangchauyan
Created May 18, 2016 07:46
Show Gist options
  • Save wangchauyan/56f2e753ee5bb93edccb8eb8ca7c611b to your computer and use it in GitHub Desktop.
Save wangchauyan/56f2e753ee5bb93edccb8eb8ca7c611b to your computer and use it in GitHub Desktop.
this is an example that shows how to use newton method by go.
package main
import (
"fmt"
"math"
)
func Newt(x float64) float64 {
if x == 0 {
return 0;
}
z := 1.0
for i := 0; i < int(x); i++ {
z = z - (math.Pow(z, 2) - x) / (2 * z)
}
return z
}
func Sqrt(x float64) float64 {
return math.Sqrt(x)
}
func main() {
fmt.Println("sqrt value", Sqrt(20))
fmt.Println("sqrt value", Newt(20))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment