Last active
August 29, 2015 13:57
-
-
Save pyk/9859076 to your computer and use it in GitHub Desktop.
my solution on A Tour of Go Exercise: Loops and Functions , http://tour.golang.org/#24
This file contains hidden or 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" | |
"math" | |
) | |
func Sqrt(x float64) float64 { | |
z := x | |
n := 0.0 | |
for math.Abs(n-z) > 1e-10 { | |
n, z = z, z - (z*z-x)/(2*z) | |
} | |
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