Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MinSomai/7e67ee7eae3557c126414e1ea31fff87 to your computer and use it in GitHub Desktop.
Save MinSomai/7e67ee7eae3557c126414e1ea31fff87 to your computer and use it in GitHub Desktop.
Intermediate Algorithm Scripting: Smallest Common Multiple
function smallestCommons(arr) {
let numbers = arr.sort((a, b)=>a-b);
for(let i = numbers[0]; i <= numbers[1]; i++){
numbers.push(i);
}
return numbers.reduce((a, b) => {
return lcm(a, b);
});
}
function gcd (a, b) {
return (a % b === 0) ? b : gcd(b, a%b);
}
function lcm (a, b) {
return (a * b) / gcd(a, b);
}
smallestCommons([1,5]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment