Kotlin Program to Find LCM of Two Numbers

The Least Common Multiple (LCM) of two numbers is the smallest number that is a multiple of both numbers. Finding the LCM is a common task in mathematics and has various applications in number theory, cryptography, and algorithm design. In this article, we will explore three different Kotlin Program to Find LCM of Two Numbers with complete code examples and outputs.

1. Using the Formula LCM(a, b) = (a * b) / GCD(a, b)

This method utilizes the relationship between the Greatest Common Divisor (GCD) and the Least Common Multiple (LCM). The formula LCM(a, b) = (a * b) / GCD(a, b) ensures that we find the smallest common multiple by first calculating the GCD of the two numbers.

Code

Kotlin
fun gcd(a: Int, b: Int): Int {
    var num1 = a
    var num2 = b
    while (num2 != 0) {
        val temp = num2
        num2 = num1 % num2
        num1 = temp
    }
    return num1
}

fun lcm(a: Int, b: Int): Int {
    return (a * b) / gcd(a, b)
}

fun main() {
    val number1 = 15
    val number2 = 20

    val result = lcm(number1, number2)
    println("LCM of $number1 and $number2 is: $result")
}

Output

Kotlin
LCM of 15 and 20 is: 60

2. Using a Loop

In this example, we find the LCM by iteratively checking multiples of the larger number until we find a common multiple of both numbers. This brute-force approach is straightforward but can be inefficient for large numbers.

Code

Kotlin
fun lcmUsingLoop(a: Int, b: Int): Int {
    var lcm = if (a > b) a else b

    while (true) {
        if (lcm % a == 0 && lcm % b == 0) {
            break
        }
        lcm++
    }
    return lcm
}

fun main() {
    val number1 = 15
    val number2 = 20

    val result = lcmUsingLoop(number1, number2)
    println("LCM of $number1 and $number2 is: $result")
}

Output

Kotlin
LCM of 15 and 20 is: 60

3. Using Kotlin’s Built-in Functions

Kotlin’s kotlin.math package provides utility functions that can be used in combination to find the LCM. This method involves leveraging existing functions to simplify the LCM calculation.

Code

Kotlin
import kotlin.math.abs

fun gcd(a: Int, b: Int): Int {
    return if (b == 0) {
        abs(a)
    } else {
        gcd(b, a % b)
    }
}

fun lcmUsingBuiltIn(a: Int, b: Int): Int {
    return abs(a * b) / gcd(a, b)
}

fun main() {
    val number1 = 15
    val number2 = 20

    val result = lcmUsingBuiltIn(number1, number2)
    println("LCM of $number1 and $number2 is: $result")
}

3.3 Output

Kotlin
LCM of 15 and 20 is: 60

4. Conclusion

Finding the LCM of two numbers is a fundamental task in mathematics and programming. Kotlin provides multiple ways to achieve this:

  • Using the Formula: This method is efficient and leverages the relationship between GCD and LCM.
  • Using a Loop: A straightforward approach that iteratively checks for common multiples, though it can be inefficient for large numbers.
  • Using Built-in Functions: Simplifies the process by using existing Kotlin functions for a clean and concise solution.

Understanding these different methods enhances your problem-solving skills and prepares you to handle various numerical tasks effectively. Each method offers a unique perspective, allowing you to choose the most suitable approach based on the specific requirements of your problem.