Bash Script to Check Prime Number with Output and Explanation
for ((i=2; i*i<=num; i++)); do if ((num % i == 0)); then echo "Not prime"; exit; fi; done; echo "Prime".Examples
How to Think About It
Algorithm
Code
#!/bin/bash read -p "Enter a number: " num if (( num < 2 )); then echo "$num is not a prime number" exit fi for ((i=2; i*i<=num; i++)); do if (( num % i == 0 )); then echo "$num is not a prime number" exit fi done echo "$num is a prime number"
Dry Run
Let's trace the number 15 through the code
Input number
num = 15
Check if less than 2
15 >= 2, continue
Loop divisors from 2 to sqrt(15) (~3.87)
Check i=2: 15 % 2 != 0; Check i=3: 15 % 3 == 0 (divisible)
Result
15 is not a prime number
| i | num % i | Divisible? |
|---|---|---|
| 2 | 15 % 2 = 1 | No |
| 3 | 15 % 3 = 0 | Yes |
Why This Works
Step 1: Check numbers less than 2
Numbers less than 2 are not prime by definition, so we handle them first with if (( num < 2 )).
Step 2: Loop up to square root
We only check divisors up to i*i <= num because any factor larger than the square root would have a smaller matching factor.
Step 3: Check divisibility
If num % i == 0, the number is divisible by i and not prime.
Step 4: Print result
If no divisors are found, the script prints that the number is prime.
Alternative Approaches
#!/bin/bash read -p "Enter a number: " num if (( num < 2 )); then echo "$num is not a prime number" exit fi i=2 while (( i*i <= num )); do if (( num % i == 0 )); then echo "$num is not a prime number" exit fi ((i++)) done echo "$num is a prime number"
#!/bin/bash read -p "Enter a number: " num if (( num < 2 )); then echo "$num is not a prime number" exit fi limit=$(echo "sqrt($num)" | bc) for ((i=2; i<=limit; i++)); do if (( num % i == 0 )); then echo "$num is not a prime number" exit fi done echo "$num is a prime number"
#!/bin/bash read -p "Enter a number: " num if (( num < 2 )); then echo "$num is not a prime number" exit fi if (( num == 2 )); then echo "$num is a prime number" exit fi if (( num % 2 == 0 )); then echo "$num is not a prime number" exit fi for ((i=3; i*i<=num; i+=2)); do if (( num % i == 0 )); then echo "$num is not a prime number" exit fi done echo "$num is a prime number"
Complexity: O(√n) time, O(1) space
Time Complexity
The script loops from 2 up to the square root of the input number, so the time grows roughly with √n.
Space Complexity
The script uses a fixed amount of memory regardless of input size, so space complexity is constant.
Which Approach is Fastest?
Skipping even numbers after checking 2 reduces iterations roughly by half, making it faster for large inputs.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Basic for loop up to sqrt | O(√n) | O(1) | Simple and clear for beginners |
| While loop up to sqrt | O(√n) | O(1) | Alternative loop style, similar performance |
| Using bc for sqrt | O(√n) | O(1) | More precise limit calculation, requires bc |
| Check 2 then odds only | O(√n/2) | O(1) | Faster for large numbers by skipping even checks |