From e353ef1858d77eaa60eeb5c9d51d9ff97917b381 Mon Sep 17 00:00:00 2001 From: britneywwc Date: Tue, 17 Oct 2023 17:57:33 +1100 Subject: [PATCH 1/9] feat: add maths square root --- maths/square_root.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 maths/square_root.ts diff --git a/maths/square_root.ts b/maths/square_root.ts new file mode 100644 index 00000000..44f1906a --- /dev/null +++ b/maths/square_root.ts @@ -0,0 +1,27 @@ +/** + * @function SquareRoot + * @description Finding the square root of a number using Newton's method. + * @param {number} num - A number. + * @param {number} precision - Precision of square root, 1e-15 by default. + * @returns {number} - Square root of the given number. + * @see https://www.geeksforgeeks.org/find-root-of-a-number-using-newtons-method/ + * @example SquareRoot(36) = 6 + * @example SquareRoot(50) = 7.0710678118654755 + */ + +export const SquareRoot = (num: number, precision: number = 1e-15): number => { + if (num < 0) throw new Error("number must be non-negative number > 0"); + if (num === 0) return 0; + + let sqrt: number = num; + let curr: number; + + while (true) { + curr = 0.5 * (sqrt + num / sqrt); + if (Math.abs(curr - sqrt) < precision) { + console.log(sqrt); + return sqrt; + } + sqrt = curr; + } +}; From 1acb094cdfeb01b87094eaa0a5dd7ff0c5ecfc33 Mon Sep 17 00:00:00 2001 From: britneywwc Date: Tue, 17 Oct 2023 17:58:03 +1100 Subject: [PATCH 2/9] test: add test for maths square root --- maths/test/square_root.test.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 maths/test/square_root.test.ts diff --git a/maths/test/square_root.test.ts b/maths/test/square_root.test.ts new file mode 100644 index 00000000..e2227034 --- /dev/null +++ b/maths/test/square_root.test.ts @@ -0,0 +1,21 @@ +import { SquareRoot } from "../square_root"; + +describe("SquareRoot", () => { + test.each([-1, -10, -2.4])( + "should throw an error for negative numbers", + (n) => { + expect(() => SquareRoot(n)).toThrow( + "number must be non-negative number > 0" + ); + } + ); + + test("should return correct square root value", () => { + expect(SquareRoot(0)).toBe(0); + expect(SquareRoot(1)).toBe(1); + expect(SquareRoot(4)).toBe(2); + expect(SquareRoot(9)).toBe(3); + expect(SquareRoot(16)).toBe(4); + expect(SquareRoot(25)).toBe(4); + }); +}); From 2ef946bd9d91a408729e2d1a2bf158775d93a60c Mon Sep 17 00:00:00 2001 From: britneywwc Date: Tue, 17 Oct 2023 17:58:31 +1100 Subject: [PATCH 3/9] docs: add square root to DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 3237c4e5..8934c233 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -124,6 +124,7 @@ * [Hexagonal Numbers.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/test/hexagonal_numbers.test.ts) * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/sieve_of_eratosthenes.ts) * [Signum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/signum.ts) + * [Square Root](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/square_root.ts) * [Ugly Numbers](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/ugly_numbers.ts) * [Zellers Congruence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/zellers_congruence.ts) From 685f3f3ecb0514dad350233681e135979a7fac26 Mon Sep 17 00:00:00 2001 From: britneywwc Date: Wed, 18 Oct 2023 10:14:41 +1100 Subject: [PATCH 4/9] test: fixed test suites PR comments --- maths/test/square_root.test.ts | 35 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/maths/test/square_root.test.ts b/maths/test/square_root.test.ts index e2227034..3dbd8658 100644 --- a/maths/test/square_root.test.ts +++ b/maths/test/square_root.test.ts @@ -1,21 +1,28 @@ -import { SquareRoot } from "../square_root"; +import { squareRoot } from "../square_root"; -describe("SquareRoot", () => { +describe("squareRoot", () => { test.each([-1, -10, -2.4])( "should throw an error for negative numbers", - (n) => { - expect(() => SquareRoot(n)).toThrow( - "number must be non-negative number > 0" - ); + (n: number) => { + expect(() => squareRoot(n)).toThrow("number must be non-negative number"); } ); - test("should return correct square root value", () => { - expect(SquareRoot(0)).toBe(0); - expect(SquareRoot(1)).toBe(1); - expect(SquareRoot(4)).toBe(2); - expect(SquareRoot(9)).toBe(3); - expect(SquareRoot(16)).toBe(4); - expect(SquareRoot(25)).toBe(4); - }); + test.each([0, 1, 4, 9, 16, 25])( + "should return correct rational square root value", + () => { + (n: number) => { + expect(() => squareRoot(n)).toBeCloseTo(Math.sqrt(n)); + }; + } + ); + + test.each([2, 15, 20, 40, 99, 10032])( + "should return correct irrational square root value", + () => { + (n: number) => { + expect(() => squareRoot(n)).toBeCloseTo(Math.sqrt(n)); + }; + } + ); }); From 96e83b13568ffc5680be4aaa554d8c863a8ec49b Mon Sep 17 00:00:00 2001 From: britneywwc Date: Wed, 18 Oct 2023 10:15:36 +1100 Subject: [PATCH 5/9] fix: removed unused code and fixed function naming --- maths/square_root.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/maths/square_root.ts b/maths/square_root.ts index 44f1906a..0b8b0a16 100644 --- a/maths/square_root.ts +++ b/maths/square_root.ts @@ -9,8 +9,8 @@ * @example SquareRoot(50) = 7.0710678118654755 */ -export const SquareRoot = (num: number, precision: number = 1e-15): number => { - if (num < 0) throw new Error("number must be non-negative number > 0"); +export const squareRoot = (num: number, precision: number = 1e-15): number => { + if (num < 0) throw new Error("number must be non-negative number"); if (num === 0) return 0; let sqrt: number = num; @@ -19,7 +19,6 @@ export const SquareRoot = (num: number, precision: number = 1e-15): number => { while (true) { curr = 0.5 * (sqrt + num / sqrt); if (Math.abs(curr - sqrt) < precision) { - console.log(sqrt); return sqrt; } sqrt = curr; From 0d7aaff93c00e91a2e6a65b7f1ad32b86f975645 Mon Sep 17 00:00:00 2001 From: britneywwc Date: Wed, 18 Oct 2023 10:22:42 +1100 Subject: [PATCH 6/9] chore: fix camelcasing and linting --- maths/test/ugly_numbers.test.ts | 14 +++++++----- maths/ugly_numbers.ts | 38 +++++++++++++++++---------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/maths/test/ugly_numbers.test.ts b/maths/test/ugly_numbers.test.ts index f55cd78c..d3dbe381 100644 --- a/maths/test/ugly_numbers.test.ts +++ b/maths/test/ugly_numbers.test.ts @@ -1,6 +1,10 @@ -import { UglyNumbers } from '../ugly_numbers'; +import { uglyNumbers } from "../ugly_numbers"; -test('Ugly Numbers', () => { - const uglyNumbers = UglyNumbers(); - expect(Array(11).fill(undefined).map(() => uglyNumbers.next().value)).toEqual([1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15]); -}) +test("Ugly Numbers", () => { + const uglyNums = uglyNumbers(); + expect( + Array(11) + .fill(undefined) + .map(() => uglyNums.next().value) + ).toEqual([1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15]); +}); diff --git a/maths/ugly_numbers.ts b/maths/ugly_numbers.ts index 5b7f60af..032ca529 100644 --- a/maths/ugly_numbers.ts +++ b/maths/ugly_numbers.ts @@ -5,32 +5,34 @@ * They can be represented in the form 2^a * 3^b * 5*c. By convention, 1 is also considered to be * an ugly number. * The first few terms of the sequence are: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20... - * + * * For the provided n, the nth ugly number shall be computed. * @see [GeeksForGeeks](https://www.geeksforgeeks.org/ugly-numbers/) */ -function* UglyNumbers() { - yield 1 +function* uglyNumbers() { + yield 1; - let idx2 = 0, idx3 = 0, idx5 = 0 - const uglyNumbers = [1] + let idx2 = 0, + idx3 = 0, + idx5 = 0; + const uglyNums: number[] = [1]; - let nextx2: number, nextx3: number, nextx5: number, nextUglyNum: number + let nextx2: number, nextx3: number, nextx5: number, nextUglyNum: number; - while(true) { - nextx2 = uglyNumbers[idx2] * 2 - nextx3 = uglyNumbers[idx3] * 3 - nextx5 = uglyNumbers[idx5] * 5 + while (true) { + nextx2 = uglyNums[idx2] * 2; + nextx3 = uglyNums[idx3] * 3; + nextx5 = uglyNums[idx5] * 5; - nextUglyNum = Math.min(nextx2, nextx3, nextx5) - yield nextUglyNum + nextUglyNum = Math.min(nextx2, nextx3, nextx5); + yield nextUglyNum; - if(nextx2 === nextUglyNum) idx2++ - if(nextx3 === nextUglyNum) idx3++ - if(nextx5 === nextUglyNum) idx5++ - - uglyNumbers.push(nextUglyNum) + if (nextx2 === nextUglyNum) idx2++; + if (nextx3 === nextUglyNum) idx3++; + if (nextx5 === nextUglyNum) idx5++; + + uglyNums.push(nextUglyNum); } } -export { UglyNumbers } +export { uglyNumbers }; From 88696d33d93a21aac1035c29d4b697c835f338f9 Mon Sep 17 00:00:00 2001 From: britneywwc Date: Wed, 18 Oct 2023 10:50:10 +1100 Subject: [PATCH 7/9] chore: updated function docs --- maths/square_root.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/square_root.ts b/maths/square_root.ts index 0b8b0a16..04a8eda0 100644 --- a/maths/square_root.ts +++ b/maths/square_root.ts @@ -1,5 +1,5 @@ /** - * @function SquareRoot + * @function squareRoot * @description Finding the square root of a number using Newton's method. * @param {number} num - A number. * @param {number} precision - Precision of square root, 1e-15 by default. From c0e375af0eeac5f7c3e8e34119c70cec0156d876 Mon Sep 17 00:00:00 2001 From: britneywwc Date: Thu, 19 Oct 2023 09:41:48 +1100 Subject: [PATCH 8/9] fix: removed ugly numbers changes --- maths/test/ugly_numbers.test.ts | 14 +++++------- maths/ugly_numbers.ts | 38 ++++++++++++++++----------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/maths/test/ugly_numbers.test.ts b/maths/test/ugly_numbers.test.ts index d3dbe381..772b2382 100644 --- a/maths/test/ugly_numbers.test.ts +++ b/maths/test/ugly_numbers.test.ts @@ -1,10 +1,6 @@ -import { uglyNumbers } from "../ugly_numbers"; +import { UglyNumbers } from '../ugly_numbers'; -test("Ugly Numbers", () => { - const uglyNums = uglyNumbers(); - expect( - Array(11) - .fill(undefined) - .map(() => uglyNums.next().value) - ).toEqual([1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15]); -}); +test('Ugly Numbers', () => { + const uglyNumbers = UglyNumbers(); + expect(Array(11).fill(undefined).map(() => uglyNumbers.next().value)).toEqual([1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15]); +}) \ No newline at end of file diff --git a/maths/ugly_numbers.ts b/maths/ugly_numbers.ts index 032ca529..527a97b0 100644 --- a/maths/ugly_numbers.ts +++ b/maths/ugly_numbers.ts @@ -5,34 +5,32 @@ * They can be represented in the form 2^a * 3^b * 5*c. By convention, 1 is also considered to be * an ugly number. * The first few terms of the sequence are: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20... - * + * * For the provided n, the nth ugly number shall be computed. * @see [GeeksForGeeks](https://www.geeksforgeeks.org/ugly-numbers/) */ -function* uglyNumbers() { - yield 1; +function* UglyNumbers() { + yield 1 - let idx2 = 0, - idx3 = 0, - idx5 = 0; - const uglyNums: number[] = [1]; + let idx2 = 0, idx3 = 0, idx5 = 0 + const uglyNumbers = [1] - let nextx2: number, nextx3: number, nextx5: number, nextUglyNum: number; + let nextx2: number, nextx3: number, nextx5: number, nextUglyNum: number - while (true) { - nextx2 = uglyNums[idx2] * 2; - nextx3 = uglyNums[idx3] * 3; - nextx5 = uglyNums[idx5] * 5; + while(true) { + nextx2 = uglyNumbers[idx2] * 2 + nextx3 = uglyNumbers[idx3] * 3 + nextx5 = uglyNumbers[idx5] * 5 - nextUglyNum = Math.min(nextx2, nextx3, nextx5); - yield nextUglyNum; + nextUglyNum = Math.min(nextx2, nextx3, nextx5) + yield nextUglyNum - if (nextx2 === nextUglyNum) idx2++; - if (nextx3 === nextUglyNum) idx3++; - if (nextx5 === nextUglyNum) idx5++; - - uglyNums.push(nextUglyNum); + if(nextx2 === nextUglyNum) idx2++ + if(nextx3 === nextUglyNum) idx3++ + if(nextx5 === nextUglyNum) idx5++ + + uglyNumbers.push(nextUglyNum) } } -export { uglyNumbers }; +export { UglyNumbers } \ No newline at end of file From eb5946f2d321192b07608a9bef7147780664fc09 Mon Sep 17 00:00:00 2001 From: britneywwc Date: Thu, 19 Oct 2023 09:44:37 +1100 Subject: [PATCH 9/9] fix: remove ugly numbers changes --- maths/test/ugly_numbers.test.ts | 2 +- maths/ugly_numbers.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/test/ugly_numbers.test.ts b/maths/test/ugly_numbers.test.ts index 772b2382..f55cd78c 100644 --- a/maths/test/ugly_numbers.test.ts +++ b/maths/test/ugly_numbers.test.ts @@ -3,4 +3,4 @@ import { UglyNumbers } from '../ugly_numbers'; test('Ugly Numbers', () => { const uglyNumbers = UglyNumbers(); expect(Array(11).fill(undefined).map(() => uglyNumbers.next().value)).toEqual([1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15]); -}) \ No newline at end of file +}) diff --git a/maths/ugly_numbers.ts b/maths/ugly_numbers.ts index 527a97b0..5b7f60af 100644 --- a/maths/ugly_numbers.ts +++ b/maths/ugly_numbers.ts @@ -33,4 +33,4 @@ function* UglyNumbers() { } } -export { UglyNumbers } \ No newline at end of file +export { UglyNumbers }