diff --git a/Maths/lcm.cjs b/Maths/lcm.cjs new file mode 100644 index 0000000000..22c0eb1e7e --- /dev/null +++ b/Maths/lcm.cjs @@ -0,0 +1,17 @@ +function gcd(a, b) { + while (b !== 0) { + ;[a, b] = [b, a % b] + } + return a +} + +function lcm(a, b) { + if (a === 0 || b === 0) return 0 + return Math.abs(a * b) / gcd(a, b) +} + +module.exports = lcm + +console.log(lcm(12, 18)) // Output: 36 +console.log(lcm(5, 0)) // Output: 0 +console.log(lcm(7, 3)) // Output: 21