algorithms/euclidian.js
2024-01-12 07:01:06 -05:00

10 lines
352 B
JavaScript

function smallestCommons(arr) {
const [min, max] = arr.sort((a, b) => a - b);
const range = Array(max - min + 1)
.fill(0)
.map((_, i) => i + min);
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
const lcm = (a, b) => (a * b) / gcd(a, b);
return range.reduce((mul, cur) => lcm(mul, cur));
}
console.log(smallestCommons([2, 10]));