Answers for "find least common multiple javascript"

0

find least common multiple javascript

function smallestCommons(arr) {
  var max = Math.max(...arr);
  var min = Math.min(...arr);
  var candidate = max;

  var smallestCommon = function(low, high) {
    // inner function to use 'high' variable
    function scm(l, h) {
      if (h % l === 0) {
        return h;
      } else {
        return scm(l, h + high);
      }
    }
    return scm(low, high);
  };

  for (var i = min; i <= max; i += 1) {
    candidate = smallestCommon(i, candidate);
  }

  return candidate;
}

smallestCommons([5, 1]); // should return 60
smallestCommons([1, 13]); // should return 360360
smallestCommons([23, 18]); //should return 6056820
Posted by: Guest on June-06-2020
0

find least common multiple javascript

function smallestCommons(arr) {
  var multiple = [];
   arr.sort(function(a, b){
    return a - b;
    
  });
  var range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step));
  multiple =range(arr[0], arr[1], 1)
  
  let max = Math.max(...multiple)
  
  const small = (current) => n % current === 0;
  let n = max * (max - 1)
  let common = false;
  common = multiple.every(small)
  while(common === false){
    n++
    common = multiple.every(small)
  }
  
  return n
}
smallestCommons([1,5]); //returns 60
Posted by: Guest on June-25-2021

Code answers related to "find least common multiple javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language