Skip to content

Instantly share code, notes, and snippets.

@nucleartide
Created November 20, 2016 02:32
Show Gist options
  • Select an option

  • Save nucleartide/e719965b466f25bbcd9df1993387579b to your computer and use it in GitHub Desktop.

Select an option

Save nucleartide/e719965b466f25bbcd9df1993387579b to your computer and use it in GitHub Desktop.
Go loops and functions exercise
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := 1.0
for i := 0;; i++ {
newZ := z - (z*z - x) / (2*x)
if math.Abs(newZ - z) < 0.00000001 {
fmt.Println("%d iterations", i)
fmt.Println("difference:", math.Sqrt(x) - newZ)
return newZ
}
z = newZ
}
}
func main() {
fmt.Println(Sqrt(2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment