Kotlin Program to Display Prime Numbers Between Two Intervals

Finding prime numbers within a specific range is a common task in programming. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In this article, we will explore different Kotlin Program to Display Prime Numbers Between Two Intervals. We will cover three different solutions, each with an example, and explain the output for each.

Prerequisites

Before diving into the examples, ensure you have the following prerequisites:

  • Kotlin Installed: You should have Kotlin installed on your system. You can download it from Kotlin’s official website.
  • IDE: An Integrated Development Environment (IDE) like IntelliJ IDEA or Android Studio for running Kotlin programs.
  • Basic Understanding of Kotlin: Familiarity with basic Kotlin syntax and concepts.

1. Simple Method to Display Prime Numbers Between Two Intervals

1.1 Explanation

The simplest way to find prime numbers between two intervals is to iterate through each number in the range and check if it is prime.

1.2 Program

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 displayPrimes(start: Int, end: Int) {
    for (number in start..end) {
        if (isPrime(number)) {
            print("$number ")
        }
    }
}

fun main() {
    val start = 10
    val end = 50
    println("Prime numbers between $start and $end are:")
    displayPrimes(start, end)
}

1.3 Output

Kotlin
Prime numbers between 10 and 50 are:
11 13 17 19 23 29 31 37 41 43 47

1.4 Explanation of Output

In this example, the isPrime function checks if each number in the range 10 to 50 is prime. The displayPrimes function prints all the prime numbers in this range.

2. Optimized Method to Display Prime Numbers Between Two Intervals

2.1 Explanation

A more efficient method involves checking for prime numbers by iterating up to the square root of each number. This reduces the number of iterations needed to determine if a number is prime.

2.2 Program

Kotlin
import kotlin.math.sqrt

fun isPrimeOptimized(number: Int): Boolean {
    if (number <= 1) return false
    for (i in 2..sqrt(number.toDouble()).toInt()) {
        if (number % i == 0) return false
    }
    return true
}

fun displayPrimesOptimized(start: Int, end: Int) {
    for (number in start..end) {
        if (isPrimeOptimized(number)) {
            print("$number ")
        }
    }
}

fun main() {
    val start = 10
    val end = 50
    println("Prime numbers between $start and $end are:")
    displayPrimesOptimized(start, end)
}

2.3 Output

Kotlin
Prime numbers between 10 and 50 are:
11 13 17 19 23 29 31 37 41 43 47

2.4 Explanation of Output

Here, the isPrimeOptimized function checks if each number in the range 10 to 50 is prime by iterating only up to the square root of the number. The displayPrimesOptimized function prints all the prime numbers in this range.

3. Using Sieve of Eratosthenes to Display Prime Numbers Between Two Intervals

3.1 Explanation

The Sieve of Eratosthenes is an efficient algorithm to find all prime numbers up to a given limit. It works by iteratively marking the multiples of each prime number starting from 2.

3.2 Program

Kotlin
fun sieveOfEratosthenes(n: Int): BooleanArray {
    val prime = BooleanArray(n + 1) { true }
    prime[0] = false
    prime[1] = false
    for (p in 2..n) {
        if (prime[p]) {
            for (i in p * p..n step p) {
                prime[i] = false
            }
        }
    }
    return prime
}

fun displayPrimesSieve(start: Int, end: Int) {
    val primeArray = sieveOfEratosthenes(end)
    for (number in start..end) {
        if (primeArray[number]) {
            print("$number ")
        }
    }
}

fun main() {
    val start = 10
    val end = 50
    println("Prime numbers between $start and $end are:")
    displayPrimesSieve(start, end)
}

3.3 Output

Kotlin
Prime numbers between 10 and 50 are:
11 13 17 19 23 29 31 37 41 43 47

3.4 Explanation of Output

In this example, the Sieve of Eratosthenes algorithm marks non-prime numbers up to 50. The displayPrimesSieve function then prints all the prime numbers in the range 10 to 50.

Conclusion

Displaying prime numbers between two intervals can be achieved through different methods, each with varying efficiencies. The simple method is straightforward but not efficient for large ranges. The optimized method reduces the number of iterations by checking up to the square root of each number. The Sieve of Eratosthenes, while more complex, is highly efficient for finding all prime numbers up to a certain limit. Depending on your requirements and the range of numbers, you can choose the method that best fits your needs.