Created
December 6, 2013 14:13
-
-
Save n3dst4/7824603 to your computer and use it in GitHub Desktop.
Sample solution for http://tour.golang.org/#24
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" | |
"math" | |
) | |
func Sqrt(x float64) (float64, int) { | |
last := 0.0 | |
z := 1.0 | |
i := 0 | |
for math.Abs(last - z) > 0.0000001 { | |
i++ | |
last = z | |
z = z - ((math.Pow(z, 2) - x) / (z * 2)) | |
} | |
return z, i | |
} | |
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