Last active
January 1, 2016 08:39
-
-
Save kaipakartik/8119133 to your computer and use it in GitHub Desktop.
My solution to the loops and functions exercise
http://tour.golang.org/#24
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" | |
"math" | |
) | |
func Sqrt(x float64) float64 { | |
next := float64(1) | |
prev := float64(0) | |
for math.Abs(next - prev) > .01 { | |
prev, next = next, next - (next*next - x)/(2*next) | |
} | |
return next | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment