Skip to content

Instantly share code, notes, and snippets.

@rhogeranacleto
Created September 19, 2017 13:08
Show Gist options
  • Save rhogeranacleto/b1f81d2d13b48bb4a509ed6ba4acc6f2 to your computer and use it in GitHub Desktop.
Save rhogeranacleto/b1f81d2d13b48bb4a509ed6ba4acc6f2 to your computer and use it in GitHub Desktop.
Calculo de Mínimo Múltiplo Comum em Javascript / Calculation of Common Minimum in Javascript
function divisibleAll(numbers, multiple) {
for (let i = 0; i < numbers.length; i++) {
if (multiple % numbers[i] !== 0) {
return false;
}
}
return true;
}
function mmc(numbers) {
numbers.sort((a, b) => {
if (a > b) {
return -1;
}
if (a < b) {
return 1;
}
return 0;
});
numbers = Array.from(new Set(numbers));
var greather = numbers.shift();
var i = 1;
while (true) {
let multiple = greather * i;
if (divisibleAll(numbers, multiple)) {
return multiple;
}
i++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment