diff --git a/math/lcm.jule b/math/lcm.jule new file mode 100644 index 0000000..e36a388 --- /dev/null +++ b/math/lcm.jule @@ -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 +} \ No newline at end of file diff --git a/math/lcm_test.jule b/math/lcm_test.jule new file mode 100644 index 0000000..d2aed6d --- /dev/null +++ b/math/lcm_test.jule @@ -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") +} \ No newline at end of file