Kotlin Program to Display Prime Numbers Between Intervals Using Function

Introduction

Understanding prime numbers and efficiently displaying them is a common task in programming. Kotlin offers a clean and concise syntax for implementing such functionality using functions. In this article, we’ll delve into how to write a Kotlin Program to Display Prime Numbers Between Intervals Using Function. We’ll explore different techniques, including simple functions and advanced algorithms like the Sieve of Eratosthenes, to achieve this goal.

1. Using Basic Functions

In this section, we’ll start with the basics of using functions to display prime numbers in Kotlin. We’ll explore two approaches: a simple function-based method and an extension function-based method.

1.1 Simple Function Approach

This approach defines a simple function to check if a number is prime and uses it to display prime numbers within a specified range.

Kotlin
fun isPrime(number: Int): Boolean {
    if (number <= 1) return false
    for (i in 2 until number) {
        if (number % i == 0) return false
    }
    return true
}

fun displayPrimesInRange(start: Int, end: Int) {
    println("Prime numbers between $start and $end are:")
    for (num in start..end) {
        if (isPrime(num)) {
            println(num)
        }
    }
}

fun main() {
    val start = 1
    val end = 20
    displayPrimesInRange(start, end)
}

Output:

Kotlin
Prime numbers between 1 and 20 are:
2
3
5
7
11
13
17
19

1.2 Extension Function Approach

Here, in this approach we will extend the Int class with a function to directly check if a number is prime, enhancing readability and usability in our prime number display program.

Kotlin
fun Int.isPrime(): Boolean {
    if (this <= 1) return false
    for (i in 2 until this) {
        if (this % i == 0) return false
    }
    return true
}

fun displayPrimesInRange(start: Int, end: Int) {
    println("Prime numbers between $start and $end are:")
    for (num in start..end) {
        if (num.isPrime()) {
            println(num)
        }
    }
}

fun main() {
    val start = 10
    val end = 30
    displayPrimesInRange(start, end)
}

Output:

Kotlin
Prime numbers between 10 and 30 are:
11
13
17
19
23
29

2. Advanced Algorithm

In this section, we’ll explore a more advanced algorithm, namely the Sieve of Eratosthenes, to efficiently find prime numbers within a given range.

2.1 Sieve of Eratosthenes

The Sieve of Eratosthenes is a highly efficient algorithm for finding prime numbers within a range. We implemented this algorithm in Kotlin to display prime numbers between specified intervals.

Kotlin
fun sieveOfEratosthenes(n: Int): List<Int> {
    val primes = mutableListOf<Boolean>().apply { repeat(n + 1) { add(true) } }
    val result = mutableListOf<Int>()

    for (p in 2..n) {
        if (primes[p]) {
            result.add(p)
            var i = p * p
            while (i <= n) {
                primes[i] = false
                i += p
            }
        }
    }
    return result
}

fun main() {
    val start = 30
    val end = 50
    val primes = sieveOfEratosthenes(end)
    val primesInRange = primes.filter { it in start..end }

    println("Prime numbers between $start and $end are:")
    println(primesInRange)
}

Output:

Kotlin
Prime numbers between 30 and 50 are:
[31, 37, 41, 43, 47]

Conclusion

In this article, we explored different approaches to display prime numbers in Kotlin using functions. The simple function approach checks each number for primality, the extension function approach extends the Int class for direct primality checks, and the Sieve of Eratosthenes algorithm efficiently finds primes within a range. Depending on the complexity of the task and the size of the interval, you can choose the most suitable method for your Kotlin projects.