Created
December 20, 2014 14:39
-
-
Save hadashiA/aa689a20928fdacd9bf1 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" | |
"math" | |
) | |
func Sqrt10(x float64) float64 { | |
var z float64 = x | |
for i := 0; i < 10; i++ { | |
z = calc(z, x) | |
} | |
return z | |
} | |
func SqrtDiff(x float64) float64 { | |
var prev float64 = x | |
var z float64 = calc(x, x) | |
for (prev - z) < 0.000001 { | |
z = calc(z, x) | |
} | |
return z | |
} | |
func calc(z float64, x float64) float64 { | |
return z - ((z * z - x) / (2 * z)) | |
} | |
func main() { | |
fmt.Println(math.Sqrt(2)) | |
fmt.Println(Sqrt10(2)) | |
fmt.Println(SqrtDiff(2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment