We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 712d428 + 054622c commit 80ed495Copy full SHA for 80ed495
Maths/SquareRoot.js
@@ -0,0 +1,19 @@
1
+/*
2
+* Author: Rak Laptudirm
3
+*
4
+* https://en.wikipedia.org/wiki/Newton%27s_method
5
6
+* Finding the square root of a number using Newton's method.
7
+*/
8
+
9
+function sqrt (num, precision = 10) {
10
+ if (!Number.isFinite(num)) { throw new TypeError(`Expected a number, received ${typeof num}`) }
11
+ if (!Number.isFinite(precision)) { throw new TypeError(`Expected a number, received ${typeof precision}`) }
12
+ let sqrt = 1
13
+ for (let i = 0; i < precision; i++) {
14
+ sqrt -= (sqrt * sqrt - num) / (2 * sqrt)
15
+ }
16
+ return sqrt
17
+}
18
19
+export { sqrt }
0 commit comments