Skip to content

Instantly share code, notes, and snippets.

@alenbasic
Created June 23, 2016 12:08
Show Gist options
  • Save alenbasic/108417acbff63d765f9da5e036fc018b to your computer and use it in GitHub Desktop.
Save alenbasic/108417acbff63d765f9da5e036fc018b to your computer and use it in GitHub Desktop.
A go-lang implementation of Newton's square root approximation.
package main
import (
"fmt"
)
// inspired by https://tour.golang.org/flowcontrol/8
func Sqrt(loop int, seed, square float64) float64 {
x := seed
for i := 0; i < loop; i++ {
x = x - ((x*x)-square)/(2*x)
}
return x
}
func main() {
fmt.Println(Sqrt(10, 1, 64))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment