]> code.delx.au - gnu-emacs-elpa/blob - packages/realgud/test/gcd.sh
497f33bc1032a8d6e09c5e443903dbd90c0548bc
[gnu-emacs-elpa] / packages / realgud / test / gcd.sh
1 #!/usr/bin/env bash
2 # Greatest Common Divisor in POSIX shell using Euclid's algorithm. On
3 # return, variable gcd_value is set and is the gcd of parameters $1
4 # and $2. The GCD of a negative number is the same as the GCD of its
5 # absolute value, since a negative number is -1 times its positive
6 # value. Negative numbers are set when there is an error; -1 is set
7 # when the wrong number of parameters are given.
8 gcd() {
9 typeset -i a=$1
10 typeset -i b=$2
11 if (( a > b )) ; then
12 a=$b
13 b=$1
14 fi
15 if (( a == 1 || (b-a) == 0)) ; then
16 gcd_value=$a
17 return 0
18 fi
19 typeset -i c
20 ((c=b-a))
21 gcd $c $a
22 }
23
24 gcd $1 $2
25 echo $gcd_value