Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions math/lcm.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn LCM(a: int, b: int): int {
if a == 0 || b == 0 {
return 0
}

let mut x = a
if x < 0 {
x = -x
}
let mut y = b
if y < 0 {
y = -y
}

return (x / GCD(x, y)) * y
}
12 changes: 12 additions & 0 deletions math/lcm_test.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#build test

use "std/testing"

#test
fn testLCM(t: &testing::T) {
t.Assert(LCM(0, 7) == 0, "lcm of zero and 7 should be 0")
t.Assert(LCM(4, 6) == 12, "lcm of 4 and 6 should be 12")
t.Assert(LCM(21, 6) == 42, "lcm of 21 and 6 should be 42")
t.Assert(LCM(-8, 12) == 24, "lcm should be positive for negative inputs")
t.Assert(LCM(-9, -6) == 18, "lcm should be positive for two negatives")
}
Loading