• Welcome to Bashguru

    Linux is one of popular version of UNIX operating System. It is open source as its source code is freely available. It is free to use. Linux was designed considering UNIX compatibility. It's functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide latest information about Linux Operating system.

  • Python Programming

    Python is a comparatively simple programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced with an overall easier to learn language with many “intuitive” features. For this reason it is common and recommended by most professionals that people new to programming start with python.

  • Perl Programming

    Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating easy manipulation of text files.

  • Android

    Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.

Thursday, August 13, 2009

Posted by venu k
5 comments | 10:35 AM
I written three different methods to calculate GCD of two numbers

Method 1:


#!/bin/bash
#*******************************************************************************************
# gcd.sh: greatest common divisor uses Eclidean algorithm
# Usage : gcd.sh number1 number2
# The algorithm used to calculate the GCD between two integers is known as the Euclidean
# algorithm(Recursive method).
#*******************************************************************************************

#----------------------------------------------------------------------
# Argument check
ARGS=2
BADARGS=65

if [ $# -ne "$ARGS" ]
then
echo ; echo "Invalid Arguments"
echo "Usage: $0 first-number second-number" ; echo
exit $BADARGS
fi

# Check supplied arguments are integers are not

isnumber()
{
if [ $1 -eq $1 2> /dev/null ]
then
:
# : is a null command.This is the shell equivalent of a "NOP"(do nothing)
else
echo -e "\nYou supplied one bad argument \"$1\" is not a number"
echo -e "Usage: $0 first-number second-number\n"
exit $BADARGS
fi
}
isnumber $1
isnumber $2

# ---------------------------------------------------------------------

function Euclidean()
{
if [ $2 -eq 0 ]
then

return $1

else

Euclidean $2 $(($1%$2))
# calling function recursively
fi
}

Euclidean $1 $2

echo "gcd of $1 and $2 is $?"

# $? returns the exit status of script. This is one method to capture return value of a function

exit 0

Method 2:


#!/bin/bash
# gcd2.sh
# Usage: gcd2.sh number1 number2
# For argument check see method 1

gcd ()
{
dividend=$1
divisor=$2
# Arbitrary assignment.
#! It doesn't matter which of the two is larger.

remainder=1

# If uninitialized variable used in loop,it results in an error message
# on the first pass through loop.

if [ $divisor -eq 0 ]
then
echo "GCD of $dividend and $divisor = $dividend"
exit 0
fi

until [ "$remainder" -eq 0 ]
do
let "remainder = $dividend % $divisor"

# Now repeat with 2 smallest numbers

dividend=$divisor .
divisor=$remainder
done
}
# Last $dividend is the gcd.

gcd $1 $2

echo; echo "GCD of $1 and $2 = $dividend"; echo
exit 0

Method 3:


#!/bin/bash
# gcd3.sh
# Usage: gcd3.sh
# This script doesn't use command line arguments.You should to enter two numbers when asking

echo enter two numbers
read n1 n2
remainder=1
# Preserve numbers for future use
t1=$n1
t2=$n2
if [ $n2 -eq 0 ]
then
echo "GCD of $n1 and $n2 = $n1"
exit 0
fi
while [ $remainder -ne 0 ]
do
remainder=`expr $n1 % $n2`
# or use
#remainder=$((n1%n2))
n1=$n2
n2=$remainder
done
echo "GCD of $t1 , $t2 is $n1"

5 comments:

  1. wow I like the recursive method. It's nice and simple

    ReplyDelete
  2. You are limited to numbers less than 256.
    You cannot return numbers greater than 255 from a function.

    ReplyDelete
  3. McAfee Online Activation
    nice post you work is good and your content is also effective to us.it is relevant to the to motive.i appreciate your work

    ReplyDelete