From 8afe254530dd356e10ffe55dd028685512f8ac80 Mon Sep 17 00:00:00 2001 From: MahaleHarsh <96824988+MahaleHarsh@users.noreply.github.com> Date: Wed, 6 Nov 2024 11:02:44 +0530 Subject: [PATCH 1/2] Update leastCommonMultiple.js applied the reduce method to iteratively compute the LCM across all numbers in the array, combining each pair of results. Additionally, I used the Euclidean algorithm for GCD calculation, which optimizes the LCM computation by reducing the multiplication size --- .../math/least-common-multiple/leastCommonMultiple.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/algorithms/math/least-common-multiple/leastCommonMultiple.js b/src/algorithms/math/least-common-multiple/leastCommonMultiple.js index 26a7681039..d8b8fe77eb 100644 --- a/src/algorithms/math/least-common-multiple/leastCommonMultiple.js +++ b/src/algorithms/math/least-common-multiple/leastCommonMultiple.js @@ -1,11 +1,12 @@ import euclideanAlgorithm from '../euclidean-algorithm/euclideanAlgorithm'; /** - * @param {number} a - * @param {number} b - * @return {number} + * Finds the LCM of an array of numbers. + * @param {number[]} arr - Array of numbers + * @return {number} - LCM of the entire array */ +export default function leastCommonMultipleArray(arr) { + if (arr.length === 0) return 0; -export default function leastCommonMultiple(a, b) { - return ((a === 0) || (b === 0)) ? 0 : Math.abs(a * b) / euclideanAlgorithm(a, b); + return arr.reduce((lcm, num) => leastCommonMultiple(lcm, num), 1); } From 8fa02b942b03eb44539d5bd8c123ce060443a098 Mon Sep 17 00:00:00 2001 From: MahaleHarsh <96824988+MahaleHarsh@users.noreply.github.com> Date: Wed, 6 Nov 2024 11:10:45 +0530 Subject: [PATCH 2/2] Update leastCommonMultiple.js