Created
August 5, 2020 07:47
-
-
Save ihpr/f34f7248977ef6de6ab35fad3dda17f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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