Created
May 11, 2019 04:18
-
-
Save tae0y/e51af57debda31c3e4d6378d1c690381 to your computer and use it in GitHub Desktop.
gotour_loop&function
This file contains 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" | |
) | |
/** | |
math.Sqrt값과 비교해 0.5이내의 오차범위내 반복 | |
연산후 정확한 값이 나오면 z가 0이 되는 것으로 종료조건을 둘 수도 있다 | |
*/ | |
func Sqrt(z float64) (float64, int) { | |
cmp := float64(math.Sqrt(z)) | |
cnt := 0 | |
for (z-cmp) > 0.5 || (z-cmp) < -0.5 { | |
z = z - (z * z - 2) / (2 * z) | |
cnt++ | |
} | |
return z, cnt | |
} | |
func main() { | |
fmt.Println(Sqrt(16)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
정답 : https://gist.github.com/tetsuok/2279991