Skip to content

Instantly share code, notes, and snippets.

@JamesTheBard
Created June 19, 2015 13:55
Show Gist options
  • Save JamesTheBard/96525b49f1dc1219867e to your computer and use it in GitHub Desktop.
Save JamesTheBard/96525b49f1dc1219867e to your computer and use it in GitHub Desktop.
Square Root Output
Starting from '10'...
5: 0.722471690979458
10: 0.7071067811865475
15: 0.7071067811865475
20: 0.7071067811865475
Starting from '100,000'...
5: 3125.00005328125
10: 97.65795665907389
20: 0.7071072944225515
30: 0.7071067811865475
package main
import (
"fmt"
)
func Sqrt(x float64, start float64, iter int) float64 {
for i := 0; i < iter; i++ {
start = .5 * (start + (x / start))
}
return start
}
func main() {
fmt.Println("Starting from '10'...")
fmt.Println("5: ", Sqrt(.5, 10, 5))
fmt.Println("10: ",Sqrt(.5, 10, 10))
fmt.Println("15: ",Sqrt(.5, 10, 15))
fmt.Println("20: ",Sqrt(.5, 10, 20))
fmt.Println("")
fmt.Println("Starting from '100,000'...")
fmt.Println("5: ", Sqrt(.5, 100000, 5))
fmt.Println("10: ", Sqrt(.5, 100000, 10))
fmt.Println("20: ", Sqrt(.5, 100000, 20))
fmt.Println("30: ", Sqrt(.5, 100000, 30))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment