package main // Compute square roots using newton's method import ( "fmt" "math" ) // We return two values: how many iterations it took to get "precise enough", and the square root itself. func sqrt(number float64) (int, float64) { approximation := 1.0 priorApproximation := 0.0 // When two successive approximations differ by less than epsilon, we're done const epsilon = 1.0e-10 counter := 0 for ; math.Abs(approximation - priorApproximation) > epsilon; counter++ { priorApproximation = approximation // 2z is the derivative of approximation^2 approximation -= (approximation * approximation - number) / (2 * approximation) } return counter, approximation } func main() { for number := 0.0; number < 101.0; number++ { var counter int var result float64 counter, result = sqrt(number) fmt.Println(number, counter, result) } }