How to find the least common multiple of a range of numbers?

前端 未结 14 1659
一个人的身影
一个人的身影 2020-12-08 08:32

Given an array of two numbers, let them define the start and end of a range of numbers. For example, [2,6] means the range 2,3,4,5,6. I want to write javascrip

相关标签:
14条回答
  • 2020-12-08 08:35

    I think this gets the job done.

    function leastCommonMultiple(min, max) {
        function range(min, max) {
            var arr = [];
            for (var i = min; i <= max; i++) {
                arr.push(i);
            }
            return arr;
        }
    
        function gcd(a, b) {
            return !b ? a : gcd(b, a % b);
        }
    
        function lcm(a, b) {
            return (a * b) / gcd(a, b);   
        }
    
        var multiple = min;
        range(min, max).forEach(function(n) {
            multiple = lcm(multiple, n);
        });
    
        return multiple;
    }
    
    leastCommonMultiple(1, 13); // => 360360
    
    0 讨论(0)
  • 2020-12-08 08:36

    Well played on the solution. I think I got one that might be abit shorter just for future reference but ill definatly look into yours

    function LCM(arrayRange) {
        var newArr = [];
    
        for (var j = arrayRange[0]; j <= arrayRange[1]; j++){
            newArr.push(j);
        }
    
        var a = Math.abs(newArr[0]);
        for (var i = 1; i < newArr.length; i++) {
            var b = Math.abs(newArr[i]),
                c = a;
    
            while (a && b) {
                a > b ? a %= b : b %= a;
            }
            a = Math.abs(c * newArr[i] / (a + b))
        }
       return console.log(a);
    }
    
    LCM([1,5]);
    
    0 讨论(0)
  • 2020-12-08 08:37

    How about:

    // Euclid Algorithm for the Greatest Common Denominator
    function gcd(a, b) {
        return !b ? a : gcd(b, a % b);
      }
      // Euclid Algorithm for the Least Common Multiple
    
    function lcm(a, b) {
        return a * (b / gcd(a, b));
      }
      // LCM of all numbers in the range of arr = [a, b];
    
    function smallestCommons(arr) {
      var i, result;
      // large to small - small to large
      if (arr[0] > arr[1]) {
        arr.reverse();
      } // only happens once. Means that the order of the arr reversed.
      for (i = result = arr[0]; i <= arr[1]; i++) { // all numbers up to arr[1] are arr[0].
        result = lcm(i, result); // lcm() makes arr int an integer because of the arithmetic operator.
      }
      return result;
    }
    smallestCommons([5, 1]); // returns 60

    0 讨论(0)
  • 2020-12-08 08:38
    function lcm(arr) {
      var max = Math.max(arr[0],arr[1]),
          min = Math.min(arr[0],arr[1]),
          lcm = max;
      var calcLcm = function(a,b){
        var mult=1;
        for(var j=1; j<=a; j++){
          mult=b*j;
          if(mult%a === 0){
            return mult;
          }
        }
      };
      for(var i=max-1;i>=min;i--){
        lcm=calcLcm(i,lcm);
      }
      return lcm;
    }
    lcm([1,13]); //should return 360360.
    
    0 讨论(0)
  • 2020-12-08 08:40
        /*Function to calculate sequential numbers 
    in the range between the arg values, both inclusive.*/
        function smallestCommons(arg1, arg2) {
         
           if(arg1>arg2) { // Swap arg1 and arg2 if arg1 is greater than arg2
              var temp = arg1;
              arg1 = arg2;
              arg2 =temp;
            }
          
          /*
          Helper function to calculate greatest common divisor (gcd)
          implementing Euclidean algorithm */
          function gcd(a, b) {
          	return b===0 ? a : gcd(b, a % b); 
           }
          
          /*
          Helper function to calculate lowest common multiple (lcm) 
    of any two numbers using gcd function above */
           function lcm(a,b){
              return (a*b)/gcd(a,b);
             }
          
          var total = arg1; // copy min value
          for(var i=arg1;i<arg2;i++){
              total = lcm(total,i+1);
             }
          //return that total
          return total;
        }
        /*Yes, there are many solutions that can get the job done.
        Check this out, same approach but different view point.
        */
        console.log(smallestCommons(13,1)); //360360
    0 讨论(0)
  • 2020-12-08 08:42

    This is a non-recursive version of your original approach.

    function smallestCommons(arr) {
      // Sort the array
      arr = arr.sort(function (a, b) {return a - b}); // numeric comparison;
      var min = arr[0];
      var max = arr[1];
    
      var numbers = [];
      var count = 0;
    
      //Here push the range of values into an array
      for (var i = min; i <= max; i++) {
        numbers.push(i);
      }
      //Here freeze a multiple candidate starting from the biggest array value - call it j
      for (var j = max; j <= 1000000; j+=max) {
    
        //I increase the denominator from min to max
        for (var k = arr[0]; k <= arr[1]; k++) {
    
          if (j % k === 0) { // every time the modulus is 0 increase a counting 
            count++; // variable
          }
        }
    
        //If the counting variable equals the lenght of the range, this candidate is the least common value
        if (count === numbers.length) { 
          return j; 
        }
        else{
          count = 0; // set count to 0 in order to test another candidate
        }
      }
    }
    
    alert(smallestCommons([1, 5]));

    0 讨论(0)
提交回复
热议问题