Skip to content

Instantly share code, notes, and snippets.

@danielramosbh74
Forked from rhogeranacleto/mmc.js
Created February 27, 2024 19:18
Show Gist options
  • Save danielramosbh74/eb357577ead77765d90fb2fe103408be to your computer and use it in GitHub Desktop.
Save danielramosbh74/eb357577ead77765d90fb2fe103408be 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