From 32b77affd39fa4fc7c10d78942432e76bd90cbca Mon Sep 17 00:00:00 2001 From: Rak Laptudirm <68542775+raklaptudirm@users.noreply.github.com> Date: Thu, 6 May 2021 10:46:27 +0530 Subject: [PATCH] Create SquareRoot.js --- Maths/SquareRoot.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Maths/SquareRoot.js diff --git a/Maths/SquareRoot.js b/Maths/SquareRoot.js new file mode 100644 index 000000000..20cdd9b4c --- /dev/null +++ b/Maths/SquareRoot.js @@ -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 }