1. Introduction

Finding the least common multiple (LCM) of numbers is a common mathematical task that arises in many programming scenarios. In this tutorial, we’ll explore how to find the LCM of two numbers in Kotlin.

2. What Is LCM

The LCM of two or more numbers is simply the smallest multiple that is evenly divisible by all the given numbers. Essentially, the least common multiple or lowest common multiple of two integers a and b, usually denoted by lcm(ab), is the smallest positive integer that is divisible by both a and b.

The formula to find the LCM of two integers is:

lcm(a, b) = |a*b| / gcd(a,b)

In this equation, lcm(a, b) signifies the smallest positive integer that is divisible by both a and b. The numerator |a*b| represents the absolute value of the product of the two integers. The denominator, gcd(a, b) corresponds to the greatest common divisor (GCD) of a and b.

3. Finding the LCM of Two Numbers

Now, let’s dive into the Kotlin code for finding the LCM of two numbers:

fun findLCM(a: Int, b: Int): Int {
    val larger = if (a > b) a else b
    val maxLcm = a * b
    var lcm = larger
    while (lcm <= maxLcm) {
        if (lcm % a == 0 && lcm % b == 0) {
            return lcm
        }
        lcm += larger
    }
    return maxLcm
}

This Kotlin code defines a function for calculating the LCM of two integers. The findLCM() function computes the LCM of two input integers, a and b, by starting with the larger of the two and iteratively checking multiples of it to see if it’s divisible by both numbers. When the potential LCM being tested is not divisible by both numbers, we’ll increase our potential LCM by the larger number and test again.

Now, we can verify the results of findLCM() with a JUnit test:

@Test
fun testLCM() {
    assertEquals(12, findLCM(3, 4))
    assertEquals(15, findLCM(5, 3))
    assertEquals(35, findLCM(7, 5))
    assertEquals(72, findLCM(24, 18))
}

4. Finding the LCM in a List of Numbers

Next, we’ll expand on this solution and learn how we can find the LCM for a list of numbers:

fun findLCMOfListOfNumbers(numbers: List<Int>): Int {
    var result = numbers[0]
    for (i in 1 until numbers.size) {
        result = findLCM(result, numbers[i])
    }
    return result
}

Therefore, to find the LCM of multiple numbers, we’ll use the findLCMOfMultipleNumbers() function, which iterates through the list of numbers, repeatedly calling the findLCM() function with the first element in the list or the most recent LCM, and the next element in the list, to compute the LCM for all numbers in the list.

Let’s verify findLCMOfListOfNumbers*()* with another test:

@Test
fun testFindLCMOfListOfNumbers() {
    assertEquals(12, findLCMOfListOfNumbers(listOf(3, 4)))
    assertEquals(15, findLCMOfListOfNumbers(listOf(5, 3)))
    assertEquals(35, findLCMOfListOfNumbers(listOf(7, 5, 5)))
    assertEquals(72, findLCMOfListOfNumbers(listOf(24, 18, 12)))
}

5. Conclusion

Finding the least common multiple of numbers is a fundamental mathematical operation. In this article, we’ve demonstrated how to calculate the LCM in Kotlin both for two integers and for many integers.

As usual, the full implementation of these examples is available over on GitHub.