Created
September 19, 2017 13:08
-
-
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
This file contains 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 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