Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Calculating the LCM of multiple numbers in JavaScript
We are required to write a JavaScript function that takes in an array of numbers of any length and returns their LCM (Least Common Multiple).
We will approach this problem in parts:
Part 1 ? We will create a helper function to calculate the Greatest Common Divisor (GCD) of two numbers
Part 2 ? Then using Part 1 helper function we will create another helper function to calculate the Least Common Multiple (LCM) of two numbers.
Part 3 ? Finally, using Part 2 helper function we will create a function that loops over the array and calculates the array LCM.
Understanding GCD and LCM
The Greatest Common Divisor (GCD) is the largest positive integer that divides both numbers. The Least Common Multiple (LCM) is the smallest positive integer that is divisible by both numbers.
The relationship between GCD and LCM is: LCM(a, b) = (a × b) / GCD(a, b)
Step 1: Calculate GCD Using Euclidean Algorithm
// Helper function to calculate GCD of two numbers
const gcd2 = (a, b) => {
// Greatest common divisor of 2 integers
if (!b) return b === 0 ? a : NaN;
return gcd2(b, a % b);
};
// Test the GCD function
console.log("GCD of 12 and 18:", gcd2(12, 18));
console.log("GCD of 15 and 20:", gcd2(15, 20));
GCD of 12 and 18: 6 GCD of 15 and 20: 5
Step 2: Calculate LCM of Two Numbers
const gcd2 = (a, b) => {
if (!b) return b === 0 ? a : NaN;
return gcd2(b, a % b);
};
// Helper function to calculate LCM of two numbers
const lcm2 = (a, b) => {
// Least common multiple of 2 integers
return a * b / gcd2(a, b);
};
// Test the LCM function
console.log("LCM of 12 and 18:", lcm2(12, 18));
console.log("LCM of 15 and 20:", lcm2(15, 20));
LCM of 12 and 18: 36 LCM of 15 and 20: 60
Step 3: Complete LCM Function for Multiple Numbers
const calculateLCM = (...arr) => {
const gcd2 = (a, b) => {
// Greatest common divisor of 2 integers
if (!b) return b === 0 ? a : NaN;
return gcd2(b, a % b);
};
const lcm2 = (a, b) => {
// Least common multiple of 2 integers
return a * b / gcd2(a, b);
};
// Least common multiple of a list of integers
let n = 1;
for (let i = 0; i < arr.length; ++i) {
n = lcm2(arr[i], n);
}
return n;
};
console.log("LCM of [12, 18, 7, 15, 20, 24, 28]:", calculateLCM(12, 18, 7, 15, 20, 24, 28));
console.log("LCM of [4, 6, 8]:", calculateLCM(4, 6, 8));
console.log("LCM of [10, 15, 25]:", calculateLCM(10, 15, 25));
LCM of [12, 18, 7, 15, 20, 24, 28]: 2520 LCM of [4, 6, 8]: 24 LCM of [10, 15, 25]: 150
How It Works
The algorithm works by:
- Using the Euclidean algorithm to find GCD recursively
- Calculating LCM using the formula: LCM(a,b) = (a × b) / GCD(a,b)
- Iteratively finding LCM of the result with each subsequent number in the array
Alternative Array Method Approach
const calculateLCMArray = (numbers) => {
const gcd = (a, b) => b === 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a * b) / gcd(a, b);
return numbers.reduce((acc, num) => lcm(acc, num), 1);
};
console.log("Using array method:", calculateLCMArray([12, 18, 7, 15, 20, 24, 28]));
Using array method: 2520
Conclusion
The LCM calculation uses the mathematical relationship between GCD and LCM, applying the Euclidean algorithm recursively. This approach efficiently handles multiple numbers by iteratively calculating LCM pairs.
