Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "math"
- )
- // Newton-Raphson method
- func Sqrt(x float64) (result float64, count int) {
- z := 1.0
- i := 0
- var prev float64 = 1.0
- init := false
- for {
- i++
- prev = z
- z -= (z * z - x ) / (2 * z)
- //fmt.Println(z, prev, prev < z + 0.00000000001)
- if z == prev || (init && prev < z + 0.00000000001) {
- break
- }
- init = true
- }
- return z, i
- }
- func main() {
- input := float64(67000000000)
- result, count := Sqrt(input)
- fmt.Println("try", result, ", count", count)
- fmt.Println("math", math.Sqrt(input))
- }
Add Comment
Please, Sign In to add comment