Skip to content

Instantly share code, notes, and snippets.

@josephok
Created October 25, 2016 04:36
Show Gist options
  • Save josephok/8ca81f55a116b8202a2ecf363cb03fdd to your computer and use it in GitHub Desktop.
Save josephok/8ca81f55a116b8202a2ecf363cb03fdd to your computer and use it in GitHub Desktop.
牛顿法求平方根
// 牛顿法求 平方根
// 如果要求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