package main
import (
"fmt"
)
// x is the number we want to know it's sqaure root.
func Sqrt(x float64) float64 {
z := 2.0 // z is the initial value we guess z^2 approches x
for i := 0; i < 10; i++ {
derivative := 2 * z // since f(z) = z^2, then f'(z) = 2 * z
delta_y := z * z - x
delta_z := delta_y / derivative
z = z - delta_z
// Above codes are equal to the following one line code
// z -= (z * z - x) / (2 * z)
fmt.Println(z)
}
return z
}
func main() {
fmt.Println(Sqrt(2))
}
student homework
python