mathball
Version:
A JavaScript library for Competitive Programming
16 lines (12 loc) • 309 B
JavaScript
;
/* Function: lcm() */
var validate = require('../validation/integer-array');
function gcd(a, b) {
return a == 0 ? b : gcd(b % a, a);
}
module.exports = function (arr) {
validate(arr, 'lcm');
return arr.reduce(function (prev, next) {
return prev * next / gcd(prev, next);
}, arr[0]);
};