Skip to content

Instantly share code, notes, and snippets.

@deep5050
Created March 25, 2018 11:57
Show Gist options
  • Save deep5050/c603485e0fa0d7d5236306bdf78d4e57 to your computer and use it in GitHub Desktop.
Save deep5050/c603485e0fa0d7d5236306bdf78d4e57 to your computer and use it in GitHub Desktop.
(((bash shell script))) 1. find prime factors of a given integer ,,2. print a pattern, 3. tells a integer is divisible by 11 or not without dividing it by 11 .
#check wheather a number is divisible by 11 or not without divide it by 11
echo "enter a number:"
read input
temp=$input
#extract digits from the number
i=0
while [ $input -ne 0 ];do
buff=`expr $input % 10`
A[$i]=$buff
i=`expr $i + 1`
input=`expr $input / 10`
done
#.................................
# even and odd sum
odd_sum=0
even_sum=0
for ((j=0;j<i;j++));do
if [ `expr $j % 2` -eq 0 ];then
odd_sum=`expr $odd_sum + ${A[$j]}`
else
even_sum=`expr $even_sum + ${A[$j]}`
fi
done
# display result
if [ $odd_sum -eq $even_sum ];then
echo "$temp is divisible by 11"
else
echo "$temp is not divisible by 11"
fi
# * * * * *
# * * *
# *
# * * *
#* * * * *
echo "enter the number of rows / columns:"
read row_col
no_astr=$row_col
no_space=0
i=1
echo ""
while [ $no_astr -gt 0 ];do
for ((i=1;i<=$no_space;i++));do
echo -n " "
done
for ((i=1;i<=$no_astr;i++));do
echo -n "* "
done
echo " "
no_astr=`expr $no_astr - 2`
no_space=`expr $no_space + 2`
done
# reverse...................
no_astr=`expr $no_astr + 4`
no_space=`expr $no_space - 4`
# echo "space = $no_space"
# echo "asterisk = $no_astr"
while [ $no_astr -le $row_col ];do
for ((i=1;i<=$no_space;i++));do
echo -n " "
done
for ((i=1;i<=$no_astr;i++));do
echo -n "* "
done
echo ""
no_astr=`expr $no_astr + 2`
no_space=`expr $no_space - 2`
done
# script to find prime factors
echo "enter an integer:"
read input
if [ $input -lt 1 ];then
echo "not allowed!"
exit 1
fi
# find factors and prime
i=2
count=0
flag=0
for ((i;i<$input;));do
if [ `expr $input % $i` -eq 0 ];then
factor=$i
for ((j=2;j<=`expr $factor / 2`;));do
flag=0
if [ `expr $factor % $j` -eq 0 ];then
flag=1
break
fi
j=`expr $j + 1`
done
if [ $flag -eq 0 ];then
echo "[ $factor ]"
count=1
fi
fi
i=`expr $i + 1`
done
if [ $count -eq 0 ];then
echo "no prime factors found except 1 and $input"
fi
@PrasadK18
Copy link

Write a program to compute Factors of a number N using prime factorization method.

Logic -> Traverse till i*i <= N instead of i <= N for efficiency.
O/P -> Print the prime factors of number N.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment