Created
October 25, 2016 04:36
-
-
Save josephok/8ca81f55a116b8202a2ecf363cb03fdd 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
// 牛顿法求 平方根 | |
// 如果要求s(s>1)的平方根, 选取1< X0 <s | |
// Xn+1 = (1/2) * (Xn + S / Xn) | |
const print = console.log | |
function sqrt(s) { | |
let [x0, i] = [1, 0] | |
while (i++ < 10) { | |
x = (1/2.0) * (x0 + s/x0) | |
x0 = x | |
} | |
return x | |
} | |
print(sqrt(3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment