// See:
// https://[Log in to view URL]
// In general, repeat:
// $$ x_{n + 1} = x_{n} - f(x_{n})/f'(x_{n}) $$
// starting from $$ x_{0} $$, an initial guess.

package main

import "fmt"

func sqrt(x float64) float64 {
    // the initial guess and the previous guess
    z, prev := 1.0, 1.0

    for i := 0; i < 50; i++ {
        prev = z        
        z -= (z*z - x) / (2*z)

        if z == prev {
            // no further improvement, have
            // reached convergence
            break
        }
    }

    return z
}

func main() {
    fmt.Println(sqrt(100))
    fmt.Println(sqrt(10000))
    fmt.Println(sqrt(0.025))
    fmt.Println(sqrt(1 << 90))
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: