From b3937649c184f78d501514ac927494ad90ab61e6 Mon Sep 17 00:00:00 2001 From: Nishant Racherla Date: Tue, 14 Feb 2023 12:40:15 -0800 Subject: [PATCH 1/7] feat(maths): added Factors --- maths/factors.ts | 24 ++++++++++++++++++++++++ maths/test/factors.test.ts | 19 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 maths/factors.ts create mode 100644 maths/test/factors.test.ts diff --git a/maths/factors.ts b/maths/factors.ts new file mode 100644 index 00000000..6e17fcd0 --- /dev/null +++ b/maths/factors.ts @@ -0,0 +1,24 @@ +/** + * @function Factors + * @description Find all the factors of a natural number. + * @param {number} num - A natural number. + * @return {number[]} - An array of all the factors of given natural number. + * @see https://en.wikipedia.org/wiki/Divisor + * @example Factors(1) = [1] + * @example Factors(4) = [1,2,4] + * @example Factors(16) = [1,3,5,15] + */ + +export const Factors = (num: number): number[] => { + if (num <= 0 || !Number.isInteger(num)) { + throw new Error("only natural numbers are supported"); + } + + const res: number[] = []; + // iterate from 1 to num and push factors into the res array + for (let i: number = 0; i <= num; i++) { + if (num % i === 0) res.push(i); + } + + return res; +}; \ No newline at end of file diff --git a/maths/test/factors.test.ts b/maths/test/factors.test.ts new file mode 100644 index 00000000..cdb6f1a0 --- /dev/null +++ b/maths/test/factors.test.ts @@ -0,0 +1,19 @@ +import { Factors } from "../factors"; + +describe("Factors", () => { + test.each([-890, -5.56, -7, 0, 0.73, 4.2, NaN, -Infinity, Infinity])( + "should throw an error for non natural number %d", + (num) => { + expect(() => Factors(num)).toThrowError( + "only natural numbers are supported", + ); + }, + ); + + test.each([[1, [1]], [2, [1,2]], [4, [1,2,4]], [6, [1,2,3,6]], [16, [1,2,4,8,16]]])( + "of %i should return the correct array of its factors", + (num, expected) => { + expect(Factors(num)).toBe(expected); + }, + ); +}); \ No newline at end of file From df1fa94e1cf838ce8f18d641d054ef141f69fb20 Mon Sep 17 00:00:00 2001 From: Nishant Racherla Date: Tue, 14 Feb 2023 12:43:29 -0800 Subject: [PATCH 2/7] fix: fixed test cases for Factors --- maths/test/factors.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/test/factors.test.ts b/maths/test/factors.test.ts index cdb6f1a0..af7cdb99 100644 --- a/maths/test/factors.test.ts +++ b/maths/test/factors.test.ts @@ -13,7 +13,7 @@ describe("Factors", () => { test.each([[1, [1]], [2, [1,2]], [4, [1,2,4]], [6, [1,2,3,6]], [16, [1,2,4,8,16]]])( "of %i should return the correct array of its factors", (num, expected) => { - expect(Factors(num)).toBe(expected); + expect(Factors(num)).toStrictEqual(expected); }, ); }); \ No newline at end of file From 478bc0fe0e954c57b9dc6dc0b898dd94ea4cb435 Mon Sep 17 00:00:00 2001 From: Nishant Racherla Date: Wed, 15 Feb 2023 09:26:55 -0800 Subject: [PATCH 3/7] fix(maths): renamed Factors to FindFactors and minor optimizations --- maths/factors.ts | 17 ++++++++++------- maths/test/factors.test.ts | 8 ++++---- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/maths/factors.ts b/maths/factors.ts index 6e17fcd0..f283336a 100644 --- a/maths/factors.ts +++ b/maths/factors.ts @@ -1,24 +1,27 @@ /** - * @function Factors + * @function FindFactors * @description Find all the factors of a natural number. * @param {number} num - A natural number. * @return {number[]} - An array of all the factors of given natural number. * @see https://en.wikipedia.org/wiki/Divisor - * @example Factors(1) = [1] - * @example Factors(4) = [1,2,4] - * @example Factors(16) = [1,3,5,15] + * @example FindFactors(1) = [1] + * @example FindFactors(4) = [1,2,4] + * @example FindFactors(16) = [1,3,5,15] */ -export const Factors = (num: number): number[] => { +export const FindFactors = (num: number): number[] => { if (num <= 0 || !Number.isInteger(num)) { throw new Error("only natural numbers are supported"); } const res: number[] = []; - // iterate from 1 to num and push factors into the res array - for (let i: number = 0; i <= num; i++) { + // iterate from 1 to square root of num + // push factors into the res array + for (let i: number = 0; i <= Math.sqrt(num); i++) { if (num % i === 0) res.push(i); } + // add num to the result array + res.push(num); return res; }; \ No newline at end of file diff --git a/maths/test/factors.test.ts b/maths/test/factors.test.ts index af7cdb99..daf4375e 100644 --- a/maths/test/factors.test.ts +++ b/maths/test/factors.test.ts @@ -1,10 +1,10 @@ -import { Factors } from "../factors"; +import { FindFactors } from "../factors"; -describe("Factors", () => { +describe("FindFactors", () => { test.each([-890, -5.56, -7, 0, 0.73, 4.2, NaN, -Infinity, Infinity])( "should throw an error for non natural number %d", (num) => { - expect(() => Factors(num)).toThrowError( + expect(() => FindFactors(num)).toThrowError( "only natural numbers are supported", ); }, @@ -13,7 +13,7 @@ describe("Factors", () => { test.each([[1, [1]], [2, [1,2]], [4, [1,2,4]], [6, [1,2,3,6]], [16, [1,2,4,8,16]]])( "of %i should return the correct array of its factors", (num, expected) => { - expect(Factors(num)).toStrictEqual(expected); + expect(FindFactors(num)).toStrictEqual(expected); }, ); }); \ No newline at end of file From 926a140b7a98cfe8f5984d55cad93021ede4e78e Mon Sep 17 00:00:00 2001 From: Nishant Racherla Date: Wed, 15 Feb 2023 09:45:19 -0800 Subject: [PATCH 4/7] fix(maths): fixed square root bug --- maths/factors.ts | 15 +++++++++++---- maths/test/factors.test.ts | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/maths/factors.ts b/maths/factors.ts index f283336a..c50f9a7e 100644 --- a/maths/factors.ts +++ b/maths/factors.ts @@ -17,11 +17,18 @@ export const FindFactors = (num: number): number[] => { const res: number[] = []; // iterate from 1 to square root of num // push factors into the res array - for (let i: number = 0; i <= Math.sqrt(num); i++) { - if (num % i === 0) res.push(i); + let i: number = 1; + while (i * i <= num) { + if (num % i === 0) { + res.push(i); + // if i is the same as num / i + const sqrtFactor = Math.floor(num / i); + if (sqrtFactor !== i) { + res.push(sqrtFactor); + } + } + i++; } - // add num to the result array - res.push(num); return res; }; \ No newline at end of file diff --git a/maths/test/factors.test.ts b/maths/test/factors.test.ts index daf4375e..1624e87f 100644 --- a/maths/test/factors.test.ts +++ b/maths/test/factors.test.ts @@ -10,7 +10,7 @@ describe("FindFactors", () => { }, ); - test.each([[1, [1]], [2, [1,2]], [4, [1,2,4]], [6, [1,2,3,6]], [16, [1,2,4,8,16]]])( + test.each([[1, [1]], [2, [1,2]], [4, [1,4,2]], [6, [1,6,2,3]], [16, [1,16,2,8,4]]])( "of %i should return the correct array of its factors", (num, expected) => { expect(FindFactors(num)).toStrictEqual(expected); From f4922c3fcd99410ce71effac98c59f9e64bde8bb Mon Sep 17 00:00:00 2001 From: zfl4wless Date: Fri, 10 Mar 2023 11:09:00 +0100 Subject: [PATCH 5/7] feat: improvements & resolves requested changes --- maths/factors.ts | 42 ++++++++++++++++++-------------------- maths/test/factors.test.ts | 36 ++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/maths/factors.ts b/maths/factors.ts index c50f9a7e..f7eaffbc 100644 --- a/maths/factors.ts +++ b/maths/factors.ts @@ -2,33 +2,31 @@ * @function FindFactors * @description Find all the factors of a natural number. * @param {number} num - A natural number. - * @return {number[]} - An array of all the factors of given natural number. + * @return {Set} - A set of all the factors of given natural number. * @see https://en.wikipedia.org/wiki/Divisor * @example FindFactors(1) = [1] * @example FindFactors(4) = [1,2,4] * @example FindFactors(16) = [1,3,5,15] */ +export const FindFactors = (num: number): Set => { + if (num <= 0 || !Number.isInteger(num)) { + throw new Error("Only natural numbers are supported."); + } -export const FindFactors = (num: number): number[] => { - if (num <= 0 || !Number.isInteger(num)) { - throw new Error("only natural numbers are supported"); - } - - const res: number[] = []; - // iterate from 1 to square root of num - // push factors into the res array - let i: number = 1; - while (i * i <= num) { - if (num % i === 0) { - res.push(i); - // if i is the same as num / i - const sqrtFactor = Math.floor(num / i); - if (sqrtFactor !== i) { - res.push(sqrtFactor); - } + const res: Set = new Set(); + // Iterates from 1 to square root of num & pushes factors into the res set. + let i: number = 1; + while (i * i <= num) { + if (num % i === 0) { + res.add(i); + // If i is the same as num / i + const sqrtFactor = Math.floor(num / i); + if (sqrtFactor !== i) { + res.add(sqrtFactor); + } + } + i++; } - i++; - } - return res; -}; \ No newline at end of file + return res; +}; diff --git a/maths/test/factors.test.ts b/maths/test/factors.test.ts index 1624e87f..7bcc4c46 100644 --- a/maths/test/factors.test.ts +++ b/maths/test/factors.test.ts @@ -1,19 +1,25 @@ import { FindFactors } from "../factors"; describe("FindFactors", () => { - test.each([-890, -5.56, -7, 0, 0.73, 4.2, NaN, -Infinity, Infinity])( - "should throw an error for non natural number %d", - (num) => { - expect(() => FindFactors(num)).toThrowError( - "only natural numbers are supported", - ); - }, - ); + test.each([-890, -5.56, -7, 0, 0.73, 4.2, NaN, -Infinity, Infinity])( + "should throw an error for non natural number %d", + (num) => { + expect(() => FindFactors(num)).toThrowError( + "Only natural numbers are supported." + ); + } + ); - test.each([[1, [1]], [2, [1,2]], [4, [1,4,2]], [6, [1,6,2,3]], [16, [1,16,2,8,4]]])( - "of %i should return the correct array of its factors", - (num, expected) => { - expect(FindFactors(num)).toStrictEqual(expected); - }, - ); -}); \ No newline at end of file + test.each([ + [1, new Set([1])], + [2, new Set([1, 2])], + [4, new Set([1, 2, 4])], + [6, new Set([1, 2, 3, 6])], + [16, new Set([1, 2, 4, 8, 16])], + ])( + "of %i should return the correct set of its factors", + (num, expected) => { + expect(FindFactors(num)).toStrictEqual(expected); + } + ); +}); From 611bd006aa72d947fee1df94cb2aea9cdf4f22a3 Mon Sep 17 00:00:00 2001 From: zfl4wless Date: Fri, 10 Mar 2023 15:10:54 +0100 Subject: [PATCH 6/7] fix: removes not needed check --- maths/factors.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/maths/factors.ts b/maths/factors.ts index f7eaffbc..8232ac3c 100644 --- a/maths/factors.ts +++ b/maths/factors.ts @@ -19,11 +19,9 @@ export const FindFactors = (num: number): Set => { while (i * i <= num) { if (num % i === 0) { res.add(i); - // If i is the same as num / i + const sqrtFactor = Math.floor(num / i); - if (sqrtFactor !== i) { - res.add(sqrtFactor); - } + res.add(sqrtFactor); } i++; } From a5c5143813bbfff6edc09d5f3cbe9cf7fd0ceec5 Mon Sep 17 00:00:00 2001 From: zfl4wless Date: Fri, 10 Mar 2023 15:12:00 +0100 Subject: [PATCH 7/7] improvement: replaces while with for loop --- maths/factors.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/maths/factors.ts b/maths/factors.ts index 8232ac3c..b9177684 100644 --- a/maths/factors.ts +++ b/maths/factors.ts @@ -15,15 +15,13 @@ export const FindFactors = (num: number): Set => { const res: Set = new Set(); // Iterates from 1 to square root of num & pushes factors into the res set. - let i: number = 1; - while (i * i <= num) { + for (let i = 1; i * i <= num; i++) { if (num % i === 0) { res.add(i); const sqrtFactor = Math.floor(num / i); res.add(sqrtFactor); } - i++; } return res;