Bash Shell Coding Exercise – Checking If Given Number is Prime


Bash Shell is powerful. We can accomplish many complex jobs based on the abundant commands. This tutorial presents a Bash shell to check a given number to see it is prime.

The simple idea of checking a number prime is to check if it only has 2 divisors 1 and itself. For example, 13 is prime but 4 is not. The number 2 is a special prime and it is the only prime that is a even number. Anything less than 2 is not a prime.

We can then test the divisors from 3 to its square root. The sqrt shell script is explained in this post. The function in Bash shell is defined as keyword function and followed by the name of the function (without any brackets for parameters). The curly brace should be on the same line that starts the definition of the function. The BASH shell is quite picky for the syntax. The function is actually a group of several commands.

Inside the function, you use $1 to reference the first parameter, $2 for the second and etc. $0 is the script filename or the function name. To return values inside function, you can just echo it and use $(func) to capture the output from outside.

We also use double brackets to start arithmetic expression tests only. The seq (more in this post) is a handy tool to generate a series of numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash
# https://helloacm.com
 
function sqrt {
        echo -e "sqrt($1)\nquit\n" | bc -q -i | head -2 | tail -1
}
 
function isPrime {
        if (($1==2)); then
                echo 1
        elif (($1<2)); then
                echo 0
        elif (($1%2==0)); then
                echo 0
        else
                for i in $(seq 3 2 `sqrt $1`); do
                        if (($1%$i==0)); then
                                echo 0
                                exit
                        fi
                done
                echo 1
        fi
}
 
[ $# -eq 0 ] && {
        echo Usage: $0 numbers
        exit
}
 
[ $(isPrime $1) -eq 0 ] && {
        echo No
} || {
        echo Yes
}
#!/bin/bash
# https://helloacm.com

function sqrt {
        echo -e "sqrt($1)\nquit\n" | bc -q -i | head -2 | tail -1
}

function isPrime {
        if (($1==2)); then
                echo 1
        elif (($1<2)); then
                echo 0
        elif (($1%2==0)); then
                echo 0
        else
                for i in $(seq 3 2 `sqrt $1`); do
                        if (($1%$i==0)); then
                                echo 0
                                exit
                        fi
                done
                echo 1
        fi
}

[ $# -eq 0 ] && {
        echo Usage: $0 numbers
        exit
}

[ $(isPrime $1) -eq 0 ] && {
        echo No
} || {
        echo Yes
}

The examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pi@raspberrypi:~$ ./prime 1
No
pi@raspberrypi:~$ ./prime 2
Yes
pi@raspberrypi:~$ ./prime 3
Yes
pi@raspberrypi:~$ ./prime 97
Yes
pi@raspberrypi:~$ ./prime 96
No
pi@raspberrypi:~$ ./prime 13
Yes
pi@raspberrypi:~$ ./prime 12
No
pi@raspberrypi:~$ 
pi@raspberrypi:~$ ./prime 1
No
pi@raspberrypi:~$ ./prime 2
Yes
pi@raspberrypi:~$ ./prime 3
Yes
pi@raspberrypi:~$ ./prime 97
Yes
pi@raspberrypi:~$ ./prime 96
No
pi@raspberrypi:~$ ./prime 13
Yes
pi@raspberrypi:~$ ./prime 12
No
pi@raspberrypi:~$ 

The above script prints 'Yes' for prime and 'No' otherwise. The simple if .. then .. else .. fi can be replaced by compound command (more in this post) for simplicity.

--EOF (The Ultimate Computing & Technology Blog) --

GD Star Rating
loading...
468 words
Last Post: Setting Up a Real Crontab for WordPress
Next Post: How to Display Top 20 Comment-ed Articles in WordPress?

The Permanent URL is: Bash Shell Coding Exercise – Checking If Given Number is Prime

Leave a Reply