Created
May 31, 2020 15:21
-
-
Save MinSomai/7e67ee7eae3557c126414e1ea31fff87 to your computer and use it in GitHub Desktop.
Intermediate Algorithm Scripting: Smallest Common Multiple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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