mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
Create SquareRoot.js
This commit is contained in:
17
Maths/SquareRoot.js
Normal file
17
Maths/SquareRoot.js
Normal 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 }
|
Reference in New Issue
Block a user