Skip to content

Commit be9300c

Browse files
authored
Merge pull request #1 from egorzhdan/master
Fix integer overflow in the LCM algorithm
2 parents 1c3cf7c + 5d6031b commit be9300c

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

src/main/io/uuddlrlrba/ktalgs/math/Gcd.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ fun gcd(a: Int, b: Int): Int {
2828
}
2929

3030
fun lcm(a: Int, b: Int): Int {
31-
return a * b / gcd(a, b)
31+
return a / gcd(a, b) * b
3232
}

src/test/io/uuddlrlrba/ktalgs/math/GcdKtTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ class GcdKtTest {
4747
assertEquals(377, lcm(13, 29))
4848
assertEquals(80, lcm(40, 16))
4949
assertEquals(120, lcm(40, 15))
50+
assertEquals(1000000, lcm(1000000, 1000000))
5051
}
5152
}

0 commit comments

Comments
 (0)