Skip to content

Instantly share code, notes, and snippets.

@rfprod
Last active April 22, 2017 15:58
Show Gist options
  • Save rfprod/8b31938b4950a83bdcd6 to your computer and use it in GitHub Desktop.
Save rfprod/8b31938b4950a83bdcd6 to your computer and use it in GitHub Desktop.
Smallest Common Multiple
function smallestCommonMultiple(arr) {
arr.sort();
var arrMaxValue = 0;
var divCounter = 0;
var minMultiplier = 0;
var commonMultiplier = 0;
arrMaxValue = Math.max.apply(Math,arr);
minMultiplier = arrMaxValue*arrMaxValue;
inputArr = [];
for (var z=arr[0];z<=arr[1];z++){
inputArr.push(z);
}
while (divCounter < inputArr.length){
for (var i=arr[0];i<=arr[1];i++){
var div = Math.abs(minMultiplier%i);
if (div > 0){
divCounter = divCounter;
}else if (div === 0){
divCounter = divCounter+1;
}
}
if (divCounter < inputArr.length){
divCounter = 0;
minMultiplier = minMultiplier + 1;
}
}
if (divCounter == inputArr.length){
commonMultiplier = minMultiplier;
}
return minMultiplier;
}
smallestCommonMultiple([1,5]);

Smallest Common Multiple

Finds the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters. The range will be an array of two numbers that will not necessarily be in numerical order.

A script by V.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment