Simple GUI Notepad Using Ruby

GUI Notepad Using Ruby Code require 'tk' class Notepad def saveFile file = File.open("note", "w") ...

Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Tuesday, April 5, 2016

Prime Factors Using Unix Shell Script

Prime Factors Using Bash Script

To do solve this problem we use the factor command available in the shell. The factor command prints the print the prime factors of the given number. The command executes as follows.

$ factor 6
6: 3 2
$ factor 5
5: 5

From the above example we can clearly see that factor n return the all prime factors of n. If the given number ‘n’ is a prime number the number of factor will be one i.e. the number itself. If the given number ‘n’ is a non prime number then the number of factor is greater than 1.

Now, we need to evaluate the factor command for each input in a to b where a is the lower range and b is the upper range given by the user. If factor i return exactly 2 arguments we will conclude that I is a prime number otherwise I is a non prime number.

For this we need to set the output of factor command to command line using set command then using the $# variable we will determine the number of values in the command line.

Code

# Unix Shell Script To Print Prime Numbers In A Given Range [a, b]

if [ $# -ne 2 ]
then
    echo "Wrong Number Of Arguments"
    exit
fi

if [ $2 -lt 0 -o $1 -lt 0 ]
then
    echo "Argument One Or Two Is Negative."
    exit
fi

a=$1
b=$2

if [ $a -gt $b ]
then
    t=$a
    a=$b
    b=$t
fi

echo "Prime Numbers In Range [$a, $b] Are:"
echo -ne "["

for i in `seq $a 1 $b`
do
    set -- `factor $i`
    if [ $# -eq 2 ]
    then
        echo -ne "$i,"
    fi
done

echo -e "\b]"

Output

$sh primerange.sh 1 10
Prime Numbers In Range [1, 10] Are:
[2,3,5,7]

$sh primerange.sh 10 50
Prime Numbers In Range [10, 50] Are:
[11,13,17,19,23,29,31,37,41,43,47]

$sh primerange.sh
Wrong Number Of Arguments

$sh primerange.sh -5 10
Argument One Or Two Is Negative.


Friday, April 1, 2016

Find Prime Numbers Using Bash Script

This script will determine all prime numbers between two positive integers a and b. a and b are supplied as command line arguments.

if [ $# -ne 2 ]
then
    echo  "Wrong Number of Arguments in the Command Line"
    exit
fi

if [ $1 -le 0 -o $2 -le 0 ]
then
    echo "Either 1st or 2nd Argument is NEGATIVE or ZERO "
    exit
fi
    
a=$1;b=$2
if [ $a -gt $b ]
then 
    t=$a
    a=$b
    b=$t
fi
i=$a
while [ $i -le $b ]
do
    set -- `factor $i | cut -f 2 -d":"`
    if [ $# -eq 1 ]
    then
        echo "$i is a Prime Number"
    fi
    i=`expr $i + 1 `
done
$ sh x.sh 2 50
2 is a Prime Number 
3 is a Prime Number 
5 is a Prime Number 
7 is a Prime Number 
11 is a Prime Number 
13 is a Prime Number 
17 is a Prime Number 
19 is a Prime Number 
23 is a Prime Number 
29 is a Prime Number 
31 is a Prime Number 
37 is a Prime Number 
41 is a Prime Number 
43 is a Prime Number 
47 is a Prime Number 

Tuesday, February 24, 2015

Bubble Sort Using Unix Shell Script

This script will sort the given input passed from command line based on bubble sort technique. You can give any number of inputs but only integers.

echo "Enter Values That You Want To Sort(eg. 3 2 1):"
read vals

set -- $vals
n=$#

k=0
for val in $* 
do
    a[$k]=$val
    ((k++))
done

flag=1

for (( i=0; i<$n-1 && $flag==1; i++ ))
do
    flag=0
    for (( j=0; j<$n-i-1; j++ ))
    do
        if [ ${a[$j]} -gt ${a[$j+1]} ]
        then
            temp=${a[$j]}
            a[$j]=${a[$j+1]}
            a[$j+1]=$temp
            flag=1
        fi
    done
done

echo "Sorted: "
for (( l=0; l<$n; l++))
do
    echo -ne "${a[$l]} "
done

echo


$ chmod +x bubble.sh
$ ./bubble.sh
Enter Values To Sort (eg. 3 2 1):
5 4 3 2 1 1 2 3 4 5
Sorted: 
1 1 2 2 3 3 4 4 5 5