Created
July 25, 2021 15:06
-
-
Save AD0791/6882b1116d615e2ac47e34a36692611f to your computer and use it in GitHub Desktop.
SQRT implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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