Skip to content

Instantly share code, notes, and snippets.

@danimal141
Last active October 10, 2016 09:05
Show Gist options
  • Save danimal141/6f428748ed492d0de69de5d652c82d8d to your computer and use it in GitHub Desktop.
Save danimal141/6f428748ed492d0de69de5d652c82d8d to your computer and use it in GitHub Desktop.
A Tour of Go Exercise: Loops and Functions
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := 1.0
for {
z2 := z - (z * z - x) / (z * 2)
if math.Abs(z2 - z) < 1e-10 {
break
}
z = z2
}
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