Friday, October 14, 2016

How to check whether a number is prime in UNIX shell script

Dear readers, thanks for your response. The pre-school blog about fabonacci series in unix shell script was a hit! 

Today we will see another script that is asked on a regular basis in various interviews and examinations. We will write a script in UNIX shell scripting to check whether a supplied number is prime or not. But, before we start lets have a look on the basics of prime number.

A number is prime, if it is divisible only by itself and 1.

Thus, to check the primality of a number n, we start dividing the with 2 and continue till n-1
Also, 2 is the only even number which is the smallest prime number. So let get down to wrote the code.


#!/bin/bash
######################################################
# SCRIPT : prime_number.bash
# USAGE  : ./prime_number.bash [Number]
# PURPOSE: Checks whether the given number is prime or not
# AUTHOR : Shashank Rawlani
######################################################


# Test the number of arguements recieved by the script

## Variable declarations
RC=1
ctr=0
num=$1

if [ $# -eq 1 ]; then

        ## Check whether the supplied number is an integer
        expr $num + 1  &> /dev/null

        RC=$?

        while [ $RC -ne 0 ]
        do

                echo "Sorry, you have supplied a non numerical value"
                echo ""
                echo -n "Enter a number: "
                read num

                ctr=`expr $ctr + 1`

               expr $num + 1  &> /dev/null
                RC=$?

                if [ $RC -ne 0 && $ctr -eq 5 ]; then
                        echo "Maximum tries reached"
                        exit 1;
                fi

        done


fi

## Actual primality test starts here 
i=2

while [ $i -lt $num ]
do
  if [ `expr $num % $i` -eq 0 ]
  then
      echo "$num is not a prime number"
      echo "Since it is divisible by $i"
      exit
  fi
  i=`expr $i + 1`
done


echo "$num is a prime number "



You can also find the script on www.rawlani.com.
Do let me know your comments below or write me on shashank@rawlani.com.

Wednesday, October 12, 2016

How to generate fabonacci series in UNIX shell scripting

It has been a long time, since I last posted a blog. It feels to be in pre-school again, since I made up my mind to write something again.
Well, unlike pre-school, we all have had the privilege to study and write the code to print the famous - "Fabonacci Series" in different scripting languages and technologies.

By definition in mathematics, the Fibonacci Numbers are the numbers in the below sequence: 0,1,1,2,3,5,8,13,21,34,55,89,144, ...... and so on...

The first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two. Some sources omit the initial 0, instead beginning the sequence with two 1s.

Thus, in mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation -
Fn = Fn-1 + Fn-2
With seed values F0 = 0 and F1 = 1.

Let us see today, how to to write and interactive program in UNIX shell script -



#!/bin/bash#!/bin/bash
#####################################################
# SCRIPT:  fabonacci.bash
# USAGE:   fabonacci.bash [Number]
# PURPOSE: Generate Fibonacci sequence till the number of iterations entered by user.
# AUTHOR:  Shashank Rawlani
#####################################################

# Test the number of arguements recieved by the script

if [ $# -eq 1 ]
then
    Num=$1
else
    echo -n "Enter the number of iterations needed :"
    read Num
fi

a=0
b=1

echo "The Fibonacci sequence for the number $Num is : "

for (( i=0;i<=Num;i++ ))
do
     echo -n "$a "
     c=$((a+b))
     a=$b
     b=$c
done


echo ""




You can also find the script on www.rawlani.com.
I will be publishing a few such UNIX scripts and a few generic scripts that I use in my daily life to help me quickly do certain tasks.

Do let me know your comments below or write me on shashank@rawlani.com.