Skip to content

Instantly share code, notes, and snippets.

@iggyster
Created August 5, 2020 07:47
Show Gist options
  • Save iggyster/f34f7248977ef6de6ab35fad3dda17f6 to your computer and use it in GitHub Desktop.
Save iggyster/f34f7248977ef6de6ab35fad3dda17f6 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
guess := float64(1)
attempts := 20
previous := float64(0)
for iteration := 0; iteration < attempts; iteration++ {
guess -= (guess*guess -x) / (2*guess)
if previous == guess {
return guess
}
previous = guess
fmt.Println(guess)
}
return guess
}
func main() {
fmt.Println(Sqrt(4))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment