Skip to content

Commit 2e4d806

Browse files
authored
feat: add square root (#208)
* feat: add maths square root * test: add test for maths square root * docs: add square root to DIRECTORY.md * test: fixed test suites PR comments * fix: removed unused code and fixed function naming * chore: fix camelcasing and linting * chore: updated function docs * fix: removed ugly numbers changes * fix: remove ugly numbers changes
1 parent 4fde4fe commit 2e4d806

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
* [Hexagonal Numbers.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/test/hexagonal_numbers.test.ts)
125125
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/sieve_of_eratosthenes.ts)
126126
* [Signum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/signum.ts)
127+
* [Square Root](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/square_root.ts)
127128
* [Ugly Numbers](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/ugly_numbers.ts)
128129
* [Zellers Congruence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/zellers_congruence.ts)
129130

maths/square_root.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @function squareRoot
3+
* @description Finding the square root of a number using Newton's method.
4+
* @param {number} num - A number.
5+
* @param {number} precision - Precision of square root, 1e-15 by default.
6+
* @returns {number} - Square root of the given number.
7+
* @see https://www.geeksforgeeks.org/find-root-of-a-number-using-newtons-method/
8+
* @example SquareRoot(36) = 6
9+
* @example SquareRoot(50) = 7.0710678118654755
10+
*/
11+
12+
export const squareRoot = (num: number, precision: number = 1e-15): number => {
13+
if (num < 0) throw new Error("number must be non-negative number");
14+
if (num === 0) return 0;
15+
16+
let sqrt: number = num;
17+
let curr: number;
18+
19+
while (true) {
20+
curr = 0.5 * (sqrt + num / sqrt);
21+
if (Math.abs(curr - sqrt) < precision) {
22+
return sqrt;
23+
}
24+
sqrt = curr;
25+
}
26+
};

maths/test/square_root.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { squareRoot } from "../square_root";
2+
3+
describe("squareRoot", () => {
4+
test.each([-1, -10, -2.4])(
5+
"should throw an error for negative numbers",
6+
(n: number) => {
7+
expect(() => squareRoot(n)).toThrow("number must be non-negative number");
8+
}
9+
);
10+
11+
test.each([0, 1, 4, 9, 16, 25])(
12+
"should return correct rational square root value",
13+
() => {
14+
(n: number) => {
15+
expect(() => squareRoot(n)).toBeCloseTo(Math.sqrt(n));
16+
};
17+
}
18+
);
19+
20+
test.each([2, 15, 20, 40, 99, 10032])(
21+
"should return correct irrational square root value",
22+
() => {
23+
(n: number) => {
24+
expect(() => squareRoot(n)).toBeCloseTo(Math.sqrt(n));
25+
};
26+
}
27+
);
28+
});

0 commit comments

Comments
 (0)