Skip to content

Instantly share code, notes, and snippets.

@afeld
Last active December 18, 2015 14:09
Show Gist options
  • Save afeld/5794928 to your computer and use it in GitHub Desktop.
Save afeld/5794928 to your computer and use it in GitHub Desktop.
my first Go exercise! Loops and Functions.
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