How do you find the lcm of a number in javascript?

The Least Common Multiple (LCM) of two integers is the smallest positive integer that is perfectly divisible by both integers.

For example, the LCM of 6 and 8 is 24.


Example 1: LCM Using while Loop and if Statement

// program to find the LCM of two integers

// take input
const num1 = prompt('Enter a first positive integer: ');
const num2 = prompt('Enter a second positive integer: ');

// higher number among number1 and number2 is stored in min
let min = (num1 > num2) ? num1 : num2;

// while loop
while (true) {
    if (min % num1 == 0 && min % num2 == 0) {
        console.log(`The LCM of ${num1} and ${num2} is ${min}`);
        break;
    }
    min++;
}

Output

Enter a first positive integer: 6
Enter a second positive integer: 8
The LCM of 6 and 8 is 24

In the above program, the user is prompted to enter two positive integers.

The greater number among the numbers provided by the user is stored in a min variable. The LCM of two numbers cannot be less than the greater number.

The while loop is used with an if statement. In each iteration,

  • The variable min is divided by both the num1 and num2.
  • If both numbers' remainders are equal to 0, then it is the LCM and the break statement terminates the program.
  • If both numbers' remainders are not equal to 0, the value of min is increased by 1 and the loop continues.
  • The while loop continues until the condition is met.
    if (min % num1 == 0 && min % num2 == 0)

The LCM of two numbers can also be found using the formula:

LCM = (num1*num2) / HCF

To learn about how to find the HCF, visit the JavaScript program to find HCF.

Example 2: LCM Calculation Using HCF

// program to find the LCM of two integers

let hcf;
// take input
const number1 = prompt('Enter a first positive integer: ');
const number2 = prompt('Enter a second positive integer: ');

// looping from 1 to number1 and number2 to find HCF
for (let i = 1; i <= number1 && i <= number2; i++) {

    // check if is factor of both integers
    if( number1 % i == 0 && number2 % i == 0) {
        hcf = i;
    }
}

// find LCM
let lcm = (number1 * number2) / hcf;

// display the hcf
console.log(`HCF of ${number1} and ${number2} is ${lcm}.`);

Output

Enter a first positive integer: 6
Enter a second positive integer: 8
The LCM of 6 and 8 is 24.

In the above program, firstly HCF of the numbers is calculated. Then LCM is calculated using the given formula.

You may have originally had a stack overflow because of a typo: you switched between min and minn in the middle of repeatRecurse (you would have caught that if repeatRecurse hadn’t been defined in the outer function). With that fixed, repeatRecurse(1,13,13) returns 156.

The obvious answer to avoiding a stack overflow is to turn a recursive function into a non-recursive function. You can accomplish that by doing:

function repeatRecurse(min, max, scm) {
    while ( min < max ) {
        while ( scm % min !== 0 ) {
            scm += max;
        }
        min++;
    }
}

But perhaps you can see the mistake at this point: you’re not ensuring that scm is still divisible by the elements that came before min. For example, repeatRecurse(3,5,5)=repeatRecurse(4,5,15)=20. Instead of adding max, you want to replace scm with its least common multiple with min. You can use rgbchris’s gcd (for integers, !b is the same thing as b===0). If you want to keep the tail optimization (although I don’t think any javascript engine has tail optimization), you’d end up with:

function repeatRecurse(min, max, scm) {
    if ( min < max ) {
        return repeatRecurse(min+1, max, lcm(scm,min));
    }
    return scm;
} 

Or without the recursion:

function repeatRecurse(min,max,scm) {
    while ( min < max ) {
        scm = lcm(scm,min);
        min++;
    }
    return scm;
}

This is essentially equivalent to rgbchris’s solution. A more elegant method may be divide and conquer:

function repeatRecurse(min,max) {
    if ( min === max ) {
        return min;
    }
    var middle = Math.floor((min+max)/2);
    return lcm(repeatRecurse(min,middle),repeatRecurse(middle+1,max));
}

I would recommend moving away from the original argument being an array of two numbers. For one thing, it ends up causing you to talk about two different arrays: [min,max] and the range array. For another thing, it would be very easy to pass a longer array and never realize you’ve done something wrong. It’s also requiring several lines of code to determine the min and max, when those should have been determined by the caller.

Finally, if you’ll be working with truly large numbers, it may be better to find the least common multiple using the prime factorization of the numbers.

How do you find the LCM of n numbers in Javascript?

The main steps of our algorithm are:.
Initialize ans = arr[0]..
Iterate over all the elements of the array i.e. from i = 1 to i = n-1. At the ith iteration ans = LCM(arr[0], arr[1], …….., arr[i-1]). This can be done easily as LCM(arr[0], arr[1], …., arr[i]) = LCM(ans, arr[i])..

How do you find an LCM of a number?

Find the LCM by listing multiples..
List the first several multiples of each number..
Look for multiples common to both lists. If there are no common multiples in the lists, write out additional multiples for each number..
Look for the smallest number that is common to both lists..
This number is the LCM..