随机组合的最小最小公倍数

Minimum least common multiplier for random combinations

TLDR: 我正在寻找一种算法,该算法 return 是可变数字数组的最小可能最小公乘数,同时知道:

我正在使用一个音乐应用程序并且遇到算法问题: 当混合不同的节奏(每个节奏都有不同的步数)时,我需要计算结果循环的步数。这可以通过最小公用乘数计算轻松完成。 假设我有一个长度数组,其中包含步骤

中所有不同的长度
var lengths = [4,5,6,8]

//greatest common denominator
function gcd(a,b){
  var t,b,a
  while(b != 0){
    t = b;
    b = a%b
    a=t
  }
  return a;
}
//least common multiplier
function lcm(a,b){
  return a*b/gcd(a,b)
}
function getLoopLength(arr{
  var result = 1;
  for(var i = 0;i<arr.length;i++)
    result = lcm(result,arr[i])
  return m
}


getLoopLength(lengths)
==> 120
// superimposing 4 rhythm with length 4,5,6 and 8 will result in a a rhythms that loops in 120 steps

现在我需要一个函数来计算以下假设的最小步数:

所以我想要的是一个看起来像这样的函数:

var minPossibleLength(knownLength, lengthsSize){
  ...
  return min
}

例如 minPossibleLength(4,4) 应该 return 24(当我的长度是 [2,4,8,3] 或 [2,4,8,6])

现在我尝试暴力破解它,遍历所有可能的长度组合并找到最小 lcm,它确实适用于我的条件,但我很想知道我是否可以找到更优雅和高效的解决方案。

感谢

minPossibleLength(4,4) 的以下算法找到了比 24 更好的解:[4, 2, 3, 6] 的最小公倍数是 12.

var lengths = [4,5,6,8]

    //greatest common denominator
    function gcd(a,b){
      var t,b,a
      while(b != 0){
        t = b;
        b = a%b
        a=t
      }
      return a;
    }
    //least common multiplier
    function lcm(a,b){
      return a*b/gcd(a,b)
    }
    function getLoopLength(arr, length){
      var result = 1;
      for(var i = 0;i<arr.length && i<length;i++)
        result = lcm(result,arr[i])
      return result
    }

    var minBound = 2;
    var maxBound = 11;

    function minPossibleLength(knownLength, lengthsSize) {      
      var min = 27720; // Maximum for bound range [2..11]
      var newmin; // Newly computed minimum.
      if (lengthsSize == 1)
        return knownLength;
      lengths[0] = knownLength;
      for(var i = minBound; i<=maxBound; i++) {
        if (i != knownLength) {
          lengths[1] = i;
          for(var j = (lengthsSize>2?i+1:maxBound); j<=maxBound; j++) {
            if (lengthsSize<3 || (i != j && j!= knownLength)) {
              lengths[2] = j;
              for(var k = (lengthsSize>3?j+1:maxBound); k<=maxBound; k++) {
                if (lengthsSize<4 || (i != k && j != k && k!= knownLength)) {
                  lengths[3] = k;
                  newmin = getLoopLength(lengths, lengthsSize)
                  if (newmin < min) {
                    min = newmin;
                    console.log('Minimum lcm so far for (['+knownLength+', '+i+(lengthsSize>2?', '+j+(lengthsSize>3?', '+k:''):'')+']) = '+min); 
                  }
                }
              }
            }
          }
        }
      }
      return min;
    }

    minPossibleLength(4,4);