Created
July 25, 2019 10:37
-
-
Save mannharleen/8f6717f892912d603699b51da4cad2ec to your computer and use it in GitHub Desktop.
Go: Newton's method for square root (using recursion)
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" | |
) | |
var z = 100.0 // the seed | |
var delta = 0.0001 // the epsilon | |
func Sqrt(x float64) float64 { | |
z = z - (z*z - x) / (2 * z) | |
if z*z-x < delta { | |
return z | |
} | |
fmt.Println("value of z = %f", z) | |
return Sqrt(x) | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use Printf
fmt.Printf("value of z = %f\n", z)
By the way, great solution! 👍