Go implementation of square root function using Newton’s method

Kasun Siyambalapitiya
1 min readAug 4, 2017

--

Image source : https://pixabay.com/en/square-root-x-roots-symbol-27349/

When you begin to learn the trending Go lang you may have encounter with the exercise to implement the square root function using Newton’s method. Here is how I implemented it.

package main

import (
"fmt"
"math"
)

func Sqrt(x float64) float64 {
var z float64=1
for i:=1;i<=10;i++{
z =(z-(math.Pow(z,2)-x)/(2*z))
}
return z
}

func main() {
fmt.Println(Sqrt(64))
}

--

--

Kasun Siyambalapitiya
Kasun Siyambalapitiya

No responses yet