在 JavaScript 中计算两个或三个数字的 LCM

Calculate the LCM of two or three numbers in JavaScript

本文关键字:三个 数字 LCM 两个 JavaScript 计算      更新时间:2023-09-26

我使用以下代码来确定两个或三个数字的最大公分母(GCD):

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]));
演示也在 JSFiddle 中

注意其中关于最小公倍数 (LCM) 的函数

我正在尝试扩展此功能,以便它可以计算相同的两个或三个用户提供的输入的LCM,但我一生都无法正确处理它。我是 JavaScript 的新手,希望得到任何帮助。请注意,如果字段留空,则也应从计算中省略该字段,就像GCD所做的那样。

您可以使用以下函数:

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<array.length; ++i)
    n = gcd2(array[i], n);
  return n;
}
function lcm2(a, b) {
  // Least common multiple of 2 integers
  return a*b / gcd2(a, b);
}
function lcm(array) {
  // Least common multiple of a list of integers
  var n = 1;
  for(var i=0; i<array.length; ++i)
    n = lcm2(array[i], n);
  return n;
}

也许你稍微改变了 GCDLCM 的结构,这样两个方法都只有两个参数。

要获取两个以上参数的结果,请使用 Array.prototype.reduce() ,它从数组中获取两个元素并返回一个结果,该结果用作新的插入,直到数组完成。

虽然 LCM 和 GCD 是关联的,但您可以根据需要将其链接起来。

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: <span id="gcd"></span><br />
LCM: <span id="lcm"></span><br />
<form name="sci-calc" method="POST" id="sci-calc">
    <input type="text" name="stuff[]" class="input-field" id="first" /><br />
    <input type="text" name="stuff[]" class="input-field" id="second" /><br />
    <input type="text" name="stuff[]" class="input-field" id="third" /><br />
    <button type="button" id="calc">CALC</button>
</form>

我还没有弄清楚你的代码在做什么,但这是查找 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
}

我知道它没有效率,但它说明了如何将它与任意数量的参数一起使用的想法(它只是递归调用)。

小提琴:https://jsfiddle.net/grabantot/fr0gzogL/