Created
July 18, 2015 22:43
-
-
Save Leko/008f23371c8ba7ff0cf5 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 ( | |
"math" | |
"fmt" | |
) | |
func Sqrt(x float64) float64 { | |
z := float64(1) | |
for i := 0; i < 100; i++ { | |
if new_z := z - (z * z - x) / (z * 2); new_z == z { | |
return z; | |
} else { | |
z = new_z | |
} | |
} | |
return z; | |
} | |
func main() { | |
fmt.Println(Sqrt(1)) | |
fmt.Println(Sqrt(2)) | |
fmt.Println(Sqrt(3)) | |
fmt.Println(Sqrt(4)) | |
fmt.Println(Sqrt(5)) | |
fmt.Println(math.Sqrt(1)) | |
fmt.Println(math.Sqrt(2)) | |
fmt.Println(math.Sqrt(3)) | |
fmt.Println(math.Sqrt(4)) | |
fmt.Println(math.Sqrt(5)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment