Skip to content

Instantly share code, notes, and snippets.

@asjadathick
Last active March 18, 2018 06:45
Show Gist options
  • Select an option

  • Save asjadathick/599cc0bcf6648be02609f729b4ccac2e to your computer and use it in GitHub Desktop.

Select an option

Save asjadathick/599cc0bcf6648be02609f729b4ccac2e to your computer and use it in GitHub Desktop.
Go code using Newton's method to find the Square Root of an integer
package main
import (
"fmt"
"math"
)
const (
TOLERANCE=0.00001
)
func Sqrt(x float64) float64 {
var z float64 = 1
for i:=0; i < 10; i++ {
y := z
if z -= (z*z - x) / (2*z); math.Abs(y-z) < TOLERANCE {
break
}
}
return z
}
func main() {
fmt.Println(Sqrt(2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment