Created
October 26, 2016 19:51
-
-
Save alexbosworth/aa6d1c9b6630c7b6d4d0a8124d670127 to your computer and use it in GitHub Desktop.
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 { | |
guess := 1.0 | |
for i := 0; i < 10; i++ { | |
guess = guess - ((math.Pow(guess, 2) - x) / (2 * guess)) | |
} | |
return guess | |
} | |
func main() { | |
x := 2.0 | |
fmt.Println("real", math.Sqrt(x), "guess", Sqrt(x)) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment