Skip to content

Instantly share code, notes, and snippets.

@Leko
Created July 18, 2015 22:43
Show Gist options
  • Save Leko/008f23371c8ba7ff0cf5 to your computer and use it in GitHub Desktop.
Save Leko/008f23371c8ba7ff0cf5 to your computer and use it in GitHub Desktop.
ニュートン法による平方根の近似解析
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