Guest User

Untitled

a guest
Oct 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "math"
  6. )
  7.  
  8. // Newton-Raphson method
  9. func Sqrt(x float64) (result float64, count int) {
  10. z := 1.0
  11. i := 0
  12. var prev float64 = 1.0
  13. init := false
  14. for {
  15. i++
  16. prev = z
  17. z -= (z * z - x ) / (2 * z)
  18. //fmt.Println(z, prev, prev < z + 0.00000000001)
  19. if z == prev || (init && prev < z + 0.00000000001) {
  20. break
  21. }
  22. init = true
  23. }
  24. return z, i
  25. }
  26.  
  27. func main() {
  28. input := float64(67000000000)
  29. result, count := Sqrt(input)
  30. fmt.Println("try", result, ", count", count)
  31. fmt.Println("math", math.Sqrt(input))
  32. }
Add Comment
Please, Sign In to add comment