From 8343b44f1d52658f8cecaffee1fd00a722f797c3 Mon Sep 17 00:00:00 2001 From: Mukund Mittal Date: Thu, 25 Jul 2024 12:16:26 +0530 Subject: [PATCH] Added LCM for Array --- .../__test__/leastCommonMultipleArray.test.js | 24 ++++++++++++++ .../leastCommonMultipleArray.js | 32 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/algorithms/math/least-common-multiple/__test__/leastCommonMultipleArray.test.js create mode 100644 src/algorithms/math/least-common-multiple/leastCommonMultipleArray.js diff --git a/src/algorithms/math/least-common-multiple/__test__/leastCommonMultipleArray.test.js b/src/algorithms/math/least-common-multiple/__test__/leastCommonMultipleArray.test.js new file mode 100644 index 00000000..11dbd531 --- /dev/null +++ b/src/algorithms/math/least-common-multiple/__test__/leastCommonMultipleArray.test.js @@ -0,0 +1,24 @@ +import leastCommonMultipleArray from '../leastCommonMultipleArray'; + +describe('leastCommonMultiple', () => { + it('should find least common multiple', () => { + expect(() => leastCommonMultipleArray([])).toThrow(Error('Array is empty')); + expect(leastCommonMultipleArray([0, 0])).toBe(0); + expect(leastCommonMultipleArray([1, 0])).toBe(0); + expect(leastCommonMultipleArray([0, 1])).toBe(0); + expect(leastCommonMultipleArray([4, 6])).toBe(12); + expect(leastCommonMultipleArray([6, 21])).toBe(42); + expect(leastCommonMultipleArray([7, 2])).toBe(14); + expect(leastCommonMultipleArray([3, 5])).toBe(15); + expect(leastCommonMultipleArray([7, 3])).toBe(21); + expect(leastCommonMultipleArray([1000000, 2])).toBe(1000000); + expect(leastCommonMultipleArray([-9, -18])).toBe(18); + expect(leastCommonMultipleArray([-7, -9])).toBe(63); + expect(leastCommonMultipleArray([-7, 9])).toBe(63); + expect(leastCommonMultipleArray([2, 3, 5])).toBe(30); + expect(leastCommonMultipleArray([2, 4, 5])).toBe(20); + expect(leastCommonMultipleArray([2, 4, 6, 8])).toBe(24); + expect(leastCommonMultipleArray([2, 4, 6, 7, 8])).toBe(168); + expect(leastCommonMultipleArray([2, 3, 5, 7, 11, 13, 17, 19])).toBe(9699690); + }); +}); diff --git a/src/algorithms/math/least-common-multiple/leastCommonMultipleArray.js b/src/algorithms/math/least-common-multiple/leastCommonMultipleArray.js new file mode 100644 index 00000000..4953315c --- /dev/null +++ b/src/algorithms/math/least-common-multiple/leastCommonMultipleArray.js @@ -0,0 +1,32 @@ +import euclideanAlgorithm from '../euclidean-algorithm/euclideanAlgorithm'; + +/** + * Function to find the least common multiple of an array of numbers. + * @param {Array} nums Array of numbers. + * @return {number} + * @throws {Error("Array is empty")} - Thrown when the input array is empty. + */ + +export default function leastCommonMultipleArray(nums) { + // Remove duplicates from array + const uniqueNums = [...new Set(nums)]; + // Checks if array is empty then throw error + if (uniqueNums.length === 0) { + throw new Error('Array is empty'); + } + // Checks if array contains 0 then return 0 as LCM + for (let i = 0; i < uniqueNums.length; i += 1) { + if (uniqueNums[i] === 0) { + return 0; + } + } + // Initialize LCM with first element of array + let lcm = Math.abs(uniqueNums[0]); + // Iterate over the array and find LCM of each element + for (let i = 1; i < uniqueNums.length; i += 1) { + const currentGCD = euclideanAlgorithm(lcm, uniqueNums[i]); + lcm = Math.abs(lcm * uniqueNums[i]) / currentGCD; + } + // Return LCM + return lcm; +}