Lcm of 3 numbers in javascript

I am using the following code to determine the Greatest Common Denominator (GCD) of two or three numbers:

Math.GCD = function(numbers) {
  for (var i = 1 ; i < numbers.length ; i++){
    if (numbers[i] || numbers[i] === 0)
      numbers[0] = twogcd(numbers[0], numbers[i]);
  }
  return numbers[0];

  function twogcd(first, second) {
    if (first < 0) first = -first;
    if (second < 0) second = -second;
    if (second > first) {var temp = first; first = second; second = temp;}
    while (true) {
        first %= second;
        if (first == 0) return second;
        second %= first;
        if (second == 0) return first;
    }
   }
};

Math.LCM = function(first,second) {
    return first * (second / this.GCD(first, second)); // CANNOT FIGURE OUT HOW TO EXTEND THIS TO THREE #s
};

// example
console.log(Math.GCD([4, 5, 10]));

Demo also in JSFiddle

Notice the function in there about the Least Common Multiple (LCM)

I am trying to extend this function so that it can compute the LCM of the same two or three user supplied inputs, but I cannot for the life of me get it right. I am a novice at JavaScript and would appreciate any help whatsoever. Note that if a field is left blank it should be omitted from the calculation also, as is done for the GCD.

Lcm of 3 numbers in javascript

KyleMit

34.4k61 gold badges427 silver badges616 bronze badges

asked Jan 22, 2016 at 18:42

1

You can use these functions:

function gcd2(a, b) {
  // Greatest common divisor of 2 integers
  if(!b) return b===0 ? a : NaN;
  return gcd2(b, a%b);
}
function gcd(array) {
  // Greatest common divisor of a list of integers
  var n = 0;
  for(var i=0; i

answered Jan 22, 2016 at 20:28

OriolOriol

257k57 gold badges407 silver badges493 bronze badges

5

Maybe you change the structure a little bit for GCDand LCM, so that both methods have only two arguments.

To get the result of more than two arguments, use Array.prototype.reduce(), which takes two elements from the array and returns a result, which is used as a new insert until the array is finished.

And while the LCM and the GCD are associative, you can chain it as desired.

Math.GCD = function twogcd(first, second) {
    if (first < 0) first = -first;
    if (second < 0) second = -second;
    if (second > first) { var temp = first; first = second; second = temp; }
    while (true) {
        first %= second;
        if (first == 0) return second;
        second %= first;
        if (second == 0) return first;
    }
};

Math.LCM = function (first, second) {
    return first * (second / Math.GCD(first, second));
};

document.getElementById('calc').addEventListener('click', function (e) {
    var first = +document.getElementById("first").value,
        second = +document.getElementById("second").value,
        third = +document.getElementById("third").value,
        numbers = [first, second, third],
        resultGCD = numbers.reduce(Math.GCD), // just chain it together
        resultLCM = numbers.reduce(Math.LCM); // just chain it together

    document.getElementById('gcd').innerHTML = resultGCD;
    document.getElementById('lcm').innerHTML = resultLCM;
});
GCD: 
LCM:



answered Jan 22, 2016 at 20:28

Lcm of 3 numbers in javascript

Nina ScholzNina Scholz

359k24 gold badges320 silver badges364 bronze badges

I haven't figured out what your code is doing but here is the function to find LCM:

LCM = function(numbers) {
  console.log(numbers)  
  if (numbers.length < 2) return
  first = numbers[0]
  second = numbers[1]
  var i = j = 1
  var mult1 = first * i++
  var mult2 = second * j++
  while (mult1 != mult2) {
    if (mult1 < mult2)
        mult1 = first * i++
    else
        mult2 = second * j++
  }
  if (numbers.length > 2) {
    numbers[1] = mult1 //I hope you're fine with the fact that 'numbers' gets modified
    mult1 = LCM(numbers.splice(1, numbers.length-1))
  }
    return mult1
}

I know It's not efficcient but it illustrates the idea of how to use it with any number of parameters (it's just called recursively).

The fiddle: https://jsfiddle.net/grabantot/fr0gzogL/

answered Jan 22, 2016 at 19:26

grabantotgrabantot

2,04316 silver badges29 bronze badges

1

How do you find the LCM of a number in Javascript?

Implementation.
let lcm = (n1, n2) => { //Find the gcd first let gcd = gcd(n1, n2); //then calculate the lcm return (n1 * n2) / gcd; } Copy..
log(lcm(15, 20)); console. log(lcm(5, 7)); Output: 60 35 Copy..
let lcm = (n1, n2) => { //Find the smallest and biggest number from both the numbers let lar = Math. ... .
Input: console..

How do you find the LCM of 3 numbers in Java?

Make n , x , s and t variables local in lcmFind . Because you need them ONLY in lcmFind method and you need to reset their values in every invocation of lcmFind ..
Make your lcmFind method static. You don't need to instantiate new object in order to calc lcm. This way you can use it like LCM..