Skip to content

Instantly share code, notes, and snippets.

@kaipakartik
Last active January 1, 2016 08:39
Show Gist options
  • Save kaipakartik/8119133 to your computer and use it in GitHub Desktop.
Save kaipakartik/8119133 to your computer and use it in GitHub Desktop.
My solution to the loops and functions exercise http://tour.golang.org/#24
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
next := float64(1)
prev := float64(0)
for math.Abs(next - prev) > .01 {
prev, next = next, next - (next*next - x)/(2*next)
}
return next
}
func main() {
fmt.Println(Sqrt(2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment