Last active
December 18, 2015 14:09
-
-
Save afeld/5794928 to your computer and use it in GitHub Desktop.
my first Go exercise! Loops and Functions.
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 ( | |
"math" | |
"fmt" | |
) | |
func Sqrt(x float64) (result float64) { | |
// Newton's method | |
result = x | |
for i := 0; i < 10; i++ { | |
result = result - ((math.Pow(result,2) - x) / (float64(2) * result)) | |
} | |
return result | |
} | |
func main() { | |
for i := 1; i < 10; i++ { | |
floatI := float64(i) | |
fmt.Printf("%v: %v, %v\n", i, Sqrt(floatI), math.Sqrt(floatI)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment