Last active
January 24, 2018 13:16
-
-
Save zhjgithub/ecc08050697ac643baadcf648f2e297a to your computer and use it in GitHub Desktop.
compute square root using formula z -= (z*z - x) / (2*z) called Newton's method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func Sqrt(x float64) float64 { | |
z := 1.0 | |
for i := 0; i < 10; i++ { | |
z -= (z*z - x) / (2 * z) | |
} | |
return z | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment