Skip to content

Instantly share code, notes, and snippets.

@ysheng26
Created November 2, 2017 16:06
Show Gist options
  • Save ysheng26/13a75047105bb070bad88fe3facd3977 to your computer and use it in GitHub Desktop.
Save ysheng26/13a75047105bb070bad88fe3facd3977 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := 1.0
var old float64
for {
old = z
z -= (z*z - x) / (2*z)
if math.Abs(old - z) < 0.00000001 {
break
}
}
return z
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(math.Sqrt(2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment