Skip to content

Instantly share code, notes, and snippets.

@jwill9999
Created October 10, 2017 16:43
Show Gist options
  • Select an option

  • Save jwill9999/8c4eb938f00b83a6c2277fff91a6e2d9 to your computer and use it in GitHub Desktop.

Select an option

Save jwill9999/8c4eb938f00b83a6c2277fff91a6e2d9 to your computer and use it in GitHub Desktop.
Smallest Common Multiple
/*
Find 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.
e.g. for 1 and 3 - find the smallest common multiple of both 1 and 3 that is evenly divisible by all numbers between 1 and 3.
*/
function smallestCommons(arr) {
var newArr = [];
var quot = 0;
var loop = 1;
var n;
var sorted = arr.sort(function(a, b) {
return b - a;
});
for (var i = arr[0]; i >= arr[1]; i--) {
newArr.push(i);
}
do {
quot = newArr[0] * loop * newArr[1];
for (n = 2; n < newArr.length; n++) {
if (quot % newArr[n] !== 0) {
break;
}
}
loop++;
} while (n !== newArr.length);
return quot;
}
smallestCommons([1, 13]);
/*
Test your code here
smallestCommons([1, 5]) should return a number.
smallestCommons([1, 5]) should return 60.
smallestCommons([5, 1]) should return 60.
smallestCommons([1, 13]) should return 360360.
smallestCommons([23, 18]) should return 6056820.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment