Skip to content

Commit 9c54dc5

Browse files
authored
Merge pull request #3 from PauLopNun/PauLopNun-patch-4
Add LCM and GCD functions in LCM.kt
2 parents ac8ecc6 + 3c5f5f0 commit 9c54dc5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/main/kotlin/math/LCM.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Computes the Least Common Multiple (LCM) of two integers.
3+
*/
4+
fun lcm(a: Int, b: Int): Int {
5+
return kotlin.math.abs(a * b) / gcd(a, b)
6+
}
7+
8+
// Reusing gcd function from GCD.kt
9+
fun gcd(a: Int, b: Int): Int {
10+
var x = a
11+
var y = b
12+
while (y != 0) {
13+
val temp = y
14+
y = x % y
15+
x = temp
16+
}
17+
return kotlin.math.abs(x)
18+
}
19+
20+
fun main() {
21+
println("LCM of 12 and 18 is ${lcm(12, 18)}") // 36
22+
println("LCM of 5 and 7 is ${lcm(5, 7)}") // 35
23+
}

0 commit comments

Comments
 (0)