Skip to content

Commit f7431a3

Browse files
author
kalvium
committed
function added
1 parent 1d252d7 commit f7431a3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Maths/lcm.cjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function gcd(a, b) {
2+
while (b !== 0) {
3+
;[a, b] = [b, a % b]
4+
}
5+
return a
6+
}
7+
8+
function lcm(a, b) {
9+
if (a === 0 || b === 0) return 0
10+
return Math.abs(a * b) / gcd(a, b)
11+
}
12+
13+
module.exports = lcm
14+
15+
console.log(lcm(12, 18)) // Output: 36
16+
console.log(lcm(5, 0)) // Output: 0
17+
console.log(lcm(7, 3)) // Output: 21

0 commit comments

Comments
 (0)