Skip to content

Instantly share code, notes, and snippets.

@AD0791
Created July 25, 2021 15:06
Show Gist options
  • Save AD0791/6882b1116d615e2ac47e34a36692611f to your computer and use it in GitHub Desktop.
Save AD0791/6882b1116d615e2ac47e34a36692611f to your computer and use it in GitHub Desktop.
SQRT implementation
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := float64(1)
for i := 1; i <= 10; i++ {
z -= (z*z-x)/(2*z)
fmt.Printf("Sqrt approximation of %v attempt %v = %v\n", x, i, z)
}
return z
}
func main() {
p := 10.0
fmt.Println(Sqrt(p)) // newton's technique
fmt.Println(math.Sqrt(p)) // validation by an inbuilt function
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment