Skip to content

Instantly share code, notes, and snippets.

@mannharleen
Created July 25, 2019 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mannharleen/8f6717f892912d603699b51da4cad2ec to your computer and use it in GitHub Desktop.
Save mannharleen/8f6717f892912d603699b51da4cad2ec to your computer and use it in GitHub Desktop.
Go: Newton's method for square root (using recursion)
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))
}
@truongtrongtin
Copy link

Use Printf
fmt.Printf("value of z = %f\n", z)
By the way, great solution! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment