Skip to content

Instantly share code, notes, and snippets.

@tarsisazevedo
Last active January 1, 2016 09:49
Show Gist options
  • Save tarsisazevedo/8127115 to your computer and use it in GitHub Desktop.
Save tarsisazevedo/8127115 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := 1.0
old_z := 0.0
delta := 0.2
for {
old_z = z
z = z - ((math.Pow(z, 2) - x) / (2 * z))
delta_newton := math.Abs(z - math.Sqrt(x))
if delta_newton <= delta || old_z == z {
return z
}
}
}
func main() {
for i := 1.0; i <= 10.0; i++ {
fmt.Println("raiz", i, Sqrt(i))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment