Understanding Least Common Multiple (LCM)Jan 2024
featured image

The Least Common Multiple, commonly abbreviated as LCM, is the smallest multiple that is divisible by two or more numbers. It's a fundamental concept in mathematics, especially when dealing with fractions, ratios, and solving problems involving multiple quantities.

In JavaScript, you can find the LCM using a function that employs the greatest common divisor (GCD) formula and the relationship between LCM and GCD: LCM(a, b) = (a * b) / GCD(a, b). Here's an example:

// Function to calculate GCD using Euclidean algorithm
function gcd(a, b) {
    if (b === 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

// Function to calculate LCM using the GCD
function lcm(a, b) {
    return (a * b) / gcd(a, b);
}

// Example usage:
const num1 = 12;
const num2 = 18;
const result = lcm(num1, num2);

console.log(`The LCM of ${num1} and ${num2} is: ${result}`);

In this example, the gcd function calculates the Greatest Common Divisor using the Euclidean algorithm, and the lcm function utilizes this GCD formula to find the Least Common Multiple.

For numbers 12 and 18:

  • GCD(12, 18) = 6

  • LCM(12, 18) = (12 * 18) / GCD(12, 18) = 36

So, the LCM of 12 and 18 is 36.