Create SquareRoot.js

This commit is contained in:
Rak Laptudirm
2021-05-06 10:46:27 +05:30
committed by GitHub
parent 3e5489d8ef
commit 32b77affd3

17
Maths/SquareRoot.js Normal file
View File

@ -0,0 +1,17 @@
/*
* Author: Rak Laptudirm
*
* https://en.wikipedia.org/wiki/Newton%27s_method
*
* Finding the square root of a number using Newton's method.
*/
function sqrt (num, precision = 10) {
let sqrt = 1
for (let i = 0; i < precision; i++) {
sqrt -= (sqrt * sqrt - num) / (2 * sqrt)
}
return sqrt
}
export { sqrt }