Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created March 15, 2017 21:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save thinkphp/b22b0f274429a39ab2f472445a3f0870 to your computer and use it in GitHub Desktop.
Computes SQuar RooT (SQRT) using recurrence relations.
/**
*
* Adrian Statescu (http://adrianstatescu.com)
*
* Computes SQuar RooT (SQRT) using recurrence relations.
*
* anplus1 = ( an + n / an ) / 2
*
* License MIT
*
*/
package main
import ("fmt"
"os"
"strconv"
"math")
func sqrt(n float64) float64 {
var an, anplus1, eps float64
eps = 0.00001
an = n / 2.0
anplus1 = ( an + n / an) / 2.0
for math.Abs( anplus1 - an ) >= eps {
an = anplus1
anplus1 = ( an + n / an) / 2.0
}
return anplus1
}
func main() {
var n float64
n,_ = strconv.ParseFloat(os.Args[1], 64)
fmt.Printf("%.10f", sqrt( n ))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment