Kotlin Program to Find GCD of Two Numbers

The Greatest Common Divisor (GCD) of two numbers is the largest number that divides both of them without leaving a remainder. Calculating the GCD is a fundamental task in number theory and has various applications in mathematics and computer science. In this article, we will explore three different Kotlin Program to Find GCD of Two Numbers with complete code examples and outputs.

1. Using the Euclidean Algorithm

The Euclidean algorithm is an efficient method for finding the GCD of two numbers. It is based on the principle that the GCD of two numbers also divides their difference. The algorithm repeatedly replaces the larger number by its remainder when divided by the smaller number until one of the numbers becomes zero. The other number at this point is the GCD.

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 main() {
    val number1 = 56
    val number2 = 98

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

Output

Kotlin
GCD of 56 and 98 is: 14

2. Using Recursion

The recursive approach to finding the GCD is a direct application of the Euclidean algorithm. Instead of using loops, the function calls itself with the new values until the base condition is met.

Code

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

fun main() {
    val number1 = 56
    val number2 = 98

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

Output

Kotlin
GCD of 56 and 98 is: 14

3. Using Kotlin’s Built-in Function

Kotlin provides a built-in function in the kotlin.math package to compute the GCD of two numbers. This method simplifies the process by leveraging the standard library’s utility functions.

Code

Kotlin
import kotlin.math.gcd

fun main() {
    val number1 = 56
    val number2 = 98

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

Output

Kotlin
GCD of 56 and 98 is: 14

4. Conclusion

Finding the GCD of two numbers is a common problem in mathematics and programming. Kotlin provides multiple ways to solve this problem, including the Euclidean algorithm, recursion, and built-in functions. Each method has its own advantages:

  • Euclidean Algorithm: Efficient and straightforward with a loop-based implementation.
  • Recursion: Provides a clean and elegant solution by directly applying the algorithm.
  • Built-in Function: Offers the simplest and most concise solution using Kotlin’s standard library.

Understanding these different approaches not only enhances your problem-solving skills but also prepares you to handle various numerical computations effectively. Each method provides a unique perspective on solving the GCD problem, allowing you to choose the most suitable approach based on your specific needs.