Created
March 25, 2018 11:53
-
-
Save deep5050/3fca8f19836788df75abb43d246817f3 to your computer and use it in GitHub Desktop.
shell script (bash) to find LCM of n integers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#find LCM of n integers | |
echo "enter the number of elements " | |
read n | |
i=0 | |
for ((i=0;i<n;i++)) | |
do | |
echo "data?" | |
read A[$i] | |
done | |
# for ((i=0;i<n;i++));do | |
# echo ${A[$i]} | |
# done | |
prev=${A[0]} | |
next=${A[1]} | |
for ((i=1;i<=n;i++));do | |
echo "..........." | |
echo "now $prev" | |
echo "next= $next" | |
echo "..........." | |
temp1=$prev | |
temp2=$next | |
#find the GCD | |
while [ $prev -ne $next ];do | |
if [ $prev -gt $next ];then | |
prev=`expr $prev - $next` | |
else | |
next=`expr $next - $prev` | |
fi | |
done | |
gcd=$prev | |
# now get the LCM | |
buff=`expr $temp1 \* $temp2` | |
lcm=`expr $buff / $gcd` | |
next=${A[$i]} | |
prev=$lcm | |
done | |
echo "LCM : $lcm" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment