Skip to content

Instantly share code, notes, and snippets.

@hadashiA
Created December 20, 2014 14:39
Show Gist options
  • Save hadashiA/aa689a20928fdacd9bf1 to your computer and use it in GitHub Desktop.
Save hadashiA/aa689a20928fdacd9bf1 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
func Sqrt10(x float64) float64 {
var z float64 = x
for i := 0; i < 10; i++ {
z = calc(z, x)
}
return z
}
func SqrtDiff(x float64) float64 {
var prev float64 = x
var z float64 = calc(x, x)
for (prev - z) < 0.000001 {
z = calc(z, x)
}
return z
}
func calc(z float64, x float64) float64 {
return z - ((z * z - x) / (2 * z))
}
func main() {
fmt.Println(math.Sqrt(2))
fmt.Println(Sqrt10(2))
fmt.Println(SqrtDiff(2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment