Skip to content

Instantly share code, notes, and snippets.

@Medeah
Created March 10, 2013 07:50
Show Gist options
  • Save Medeah/5127545 to your computer and use it in GitHub Desktop.
Save Medeah/5127545 to your computer and use it in GitHub Desktop.
A Tour of Go 23: Exercise: Loops and Functions
package main
import (
"fmt"
"math"
)
// square root function using Newton's method
func Sqrt(x float64) float64 {
z := 1.0
for math.Abs(z*z-x) > 1e-10 {
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