Skip to content

Instantly share code, notes, and snippets.

@alexbosworth
Created October 26, 2016 19:51
Show Gist options
  • Save alexbosworth/aa6d1c9b6630c7b6d4d0a8124d670127 to your computer and use it in GitHub Desktop.
Save alexbosworth/aa6d1c9b6630c7b6d4d0a8124d670127 to your computer and use it in GitHub Desktop.
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