Showing posts with label My Work. Show all posts
Showing posts with label My Work. Show all posts

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.

Thursday, October 9, 2014

The neverending distance and the tenacity of love!

How long can you wait for someone? A day, a month, probably a couple of years! But, how can you decide or conclude? When to end the prolonged wait? How far can you stretch to keep the relationship on? Why does the distance between you keeps on increasing? On every step closer, you are pushed miles apart!

The neverending distance and the tenacity of love!
The two things that a long distance relationship has to live with, until someone takes a leap and does something to cut the distance short. The one, hard step, that sets things apart and makes way for the one, for whom, you have been waiting. The one that makes you happy, truly happy.

Whatever you do to make things right, sometimes they don't matter. Your efforts are not counted as EFFORTS, while rest of the things, make no sense and sometimes, things go South!
What to do in those times? How you handle those situations? How you bring things, back on track? And the most important thing is, how to channel all your emotions, LOVE, ANGER, AGONY, DESPAIR, etc., to the right direction and in the same time express yourself.


Love is all about the journey and your actions, define your relation and vice versa. 

It has been several long years, of togetherness, without actually being together. A lot of ups and downs have kept our lives interesting, happening, a bit slow and sometimes a bit low. But through these years, one thing that has been there & has constantly kept this relation alive was "THE JOURNEY". Love is all about the journey, the little rides of emotions and a bunch of rants and ramblings...

Please share your comments and feedbacks.
If you too have a love story, share a few lines on our Facebook page with #MyLoveStoryMyWords, a teaser with a few lines in your own words, a trailer(a BiiiiiG teaser), or a sneak peak with a small video in your own way or a quotation(with a courtesy tag, if copied). Check out my teaser.

Do remember, love is not restricted to your ought to be! It can be about the one, about that one, your BFF, your family members, it can even be hypothetical, any one of them.

Keep reading, keep sharing and keep the life-lanterns fueled up!

Saturday, September 20, 2014

My Story.. A glimpse from the past..

The last week was quite a roller coaster ride for me. A lot of rises and falls, really rocked their way into my world and into my work. They sometimes, gave me a sense of numbness and on the other hand, peace of mind and carelessness. These events gave me a flashback of tons of memories from past few years and one thing lead to another, and I ended up on my collection of poems. Since I am not a frequent writer, I would share, some of my work from different genre, on this blog.

Starting with my very first step, the baby-step, into the world of literature and writings, my first ever poem. My आगाज़ -


ये ज़िन्दगी भी कितनी अजीब है यहाँ

ये ज़िन्दगी भी कितनी अजीब है यहाँ, 
मौसम बदलते है, semesters  की तरह । 

teachers का मूड भी उन, seasonal winds की तरह है, 
जो कभी ठंडी, कभी गरम, क्या पता, कब किस तरह।  

वो किताबें हमारी, जिनसे लगती हमें बिमारी,
लेते थे, बस कुछ एक dose, भूले भटके, याद आया तो, कही किसी रोज़ । 

वो mess, जहा मिलता था , कुछ कुछ खाने जैसा,
क्या अरहर, क्या मूंग, सब कुछ बस, एक जैसा । 

रोटियों की शक्ल देख कर ही मन्न भर जाता था 
हमारे हॉस्टल का पालतू कुत्ता भी, उसे अपना पेट भरने क लिए खाता था । 


Now during  the  college timings कुछ ऐसा होता था -


वो सुबह की गर्मी, वो पढ़ाई में नरमी,
वो class की खिड़कियाँ, और बाहर टहलती college की लडकियां । 

फिर teachers की cross - questioning
then bunk & canteen । 

फिर आते थे M. S. T. (ease ), बस ख़त्म हमारी सारी ease,
then पढ़ाई की tensions , जो करती हमें decrease । 

फिर results का suspense , क्या बताऊँ यारों, I was so tense 
But I was sure about my clearance । 

पर घबराता था, कभी - कभी,
हो जाए न कोई incidence । 

But I got through , 
with percentage 82 [ Just to rhyme better ] । 

फिर हुई placement,
that was something I meant । 

फिर काम का भोझ, और अपने लिए वक़्त की खोज!

ये ज़िन्दगी  अजीब है यहाँ,
क्या पता किस मोड़ पे, राही मिले कब किस तरह। 

- शशांक रावलानी 


Please share your comments and feedback below and other social platforms: Google +, Facebook and Twitter.