0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Check Prime Number with Output

You can check if a number is prime in Kotlin by writing a function that tests divisibility from 2 up to the square root of the number using for (i in 2..sqrt(n.toDouble()).toInt()) and returns false if any divisor is found, otherwise true.
📋

Examples

Input2
Output2 is a prime number
Input15
Output15 is not a prime number
Input17
Output17 is a prime number
🧠

How to Think About It

To check if a number is prime, think about whether it can be divided evenly by any number other than 1 and itself. You only need to check divisors up to the square root of the number because if a larger factor exists, a smaller one must also exist. If no divisors are found, the number is prime.
📐

Algorithm

1
Get the input number.
2
If the number is less than 2, return that it is not prime.
3
Check divisibility from 2 up to the square root of the number.
4
If any divisor divides the number evenly, return not prime.
5
If no divisors found, return prime.
💻

Code

kotlin
import kotlin.math.sqrt

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

fun main() {
    val number = 17
    if (isPrime(number)) {
        println("$number is a prime number")
    } else {
        println("$number is not a prime number")
    }
}
Output
17 is a prime number
🔍

Dry Run

Let's trace the number 17 through the code to see how it checks for primality.

1

Check if number is less than 2

17 is not less than 2, so continue.

2

Calculate square root

Square root of 17 is approximately 4.12, so check divisors from 2 to 4.

3

Check divisibility by 2

17 % 2 = 1 (not zero), continue.

4

Check divisibility by 3

17 % 3 = 2 (not zero), continue.

5

Check divisibility by 4

17 % 4 = 1 (not zero), no divisors found.

6

Return result

No divisors found, so 17 is prime.

Divisor17 % DivisorDivides Evenly?
21No
32No
41No
💡

Why This Works

Step 1: Check for numbers less than 2

Numbers less than 2 are not prime by definition, so we return false immediately.

Step 2: Limit divisor checks to square root

Checking divisors only up to the square root reduces work because any factor larger than the square root pairs with a smaller factor already checked.

Step 3: Return true if no divisors found

If no number divides evenly, the number is prime, so we return true.

🔄

Alternative Approaches

Check divisibility up to n-1
kotlin
fun isPrime(n: Int): Boolean {
    if (n < 2) return false
    for (i in 2 until n) {
        if (n % i == 0) return false
    }
    return true
}
This method is simpler but slower because it checks more numbers than necessary.
Use recursion to check divisors
kotlin
fun isPrime(n: Int, i: Int = 2): Boolean {
    if (n < 2) return false
    if (i > kotlin.math.sqrt(n.toDouble()).toInt()) return true
    if (n % i == 0) return false
    return isPrime(n, i + 1)
}
This recursive approach is elegant but may be less efficient and harder to read for beginners.

Complexity: O(√n) time, O(1) space

Time Complexity

The loop runs up to the square root of n, so the time grows roughly with √n.

Space Complexity

Only a few variables are used, so space is constant O(1).

Which Approach is Fastest?

Checking up to the square root is fastest among simple methods; checking up to n-1 is slower, and recursion adds overhead.

ApproachTimeSpaceBest For
Check up to √nO(√n)O(1)Efficient and simple
Check up to n-1O(n)O(1)Simple but slower
Recursive checkO(√n)O(√n) due to call stackElegant but less efficient
💡
Always check divisors only up to the square root of the number to improve efficiency.
⚠️
Beginners often forget to handle numbers less than 2, which are not prime.