Kotlin Program to Check Whether a Number is Prime or Not

Kotlin is a modern, concise, and safe programming language that is fully interoperable with Java. One common task in programming is to determine if a number is prime. 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 Kotlin Program to Check Whether a Number is Prime or Not. We will cover three different methods, 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 Check Prime Number

1.1 Explanation

The simplest way to check if a number is prime is by iterating from 2 to the number-1 and checking if the number is divisible by any of these. If it is, then it’s not a prime number.

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 main() {
    val number = 29
    if (isPrime(number)) {
        println("$number is a prime number.")
    } else {
        println("$number is not a prime number.")
    }
}

1.3 Output

Kotlin
29 is a prime number.

1.4 Explanation of Output

In this example, the function isPrime checks if the number 29 is prime. It iterates from 2 to 28 and finds no divisor, concluding that 29 is a prime number.

2. Optimized Method to Check Prime Number

2.1 Explanation

A more efficient method involves iterating up to the square root of the number. If the number is not divisible by any number up to its square root, it is a prime number. This reduces the number of iterations.

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 main() {
    val number = 29
    if (isPrimeOptimized(number)) {
        println("$number is a prime number.")
    } else {
        println("$number is not a prime number.")
    }
}

2.3 Output

Kotlin
29 is a prime number.

2.4 Explanation of Output

Here, the function isPrimeOptimized checks up to the square root of 29. Since there are no divisors within this range, it determines that 29 is a prime number.

3. Using Sieve of Eratosthenes

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 main() {
    val n = 30
    val primeArray = sieveOfEratosthenes(n)
    val number = 29
    if (primeArray[number]) {
        println("$number is a prime number.")
    } else {
        println("$number is not a prime number.")
    }
}

3.3 Output

Kotlin
29 is a prime number.

3.4 Explanation of Output

In this example, the Sieve of Eratosthenes algorithm marks non-prime numbers up to 30. It then checks if 29 is marked as prime, which it is, confirming that 29 is a prime number.

Conclusion

Checking whether a number is prime can be done in several ways, each with different efficiencies. The simple method is straightforward but not efficient for large numbers. The optimized method significantly reduces the number of iterations by checking up to the square root of the number. The Sieve of Eratosthenes, while more complex, is highly efficient for finding all prime numbers up to a certain limit. Depending on the use case and performance requirements, you can choose the method that best fits your needs.