sh (((bash shell script)))1。找到给定整数的素数因子,2。打印一个模式,3。告诉一个整数可以被11整除或不整除

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh (((bash shell script)))1。找到给定整数的素数因子,2。打印一个模式,3。告诉一个整数可以被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

以上是关于sh (((bash shell script)))1。找到给定整数的素数因子,2。打印一个模式,3。告诉一个整数可以被11整除或不整除的主要内容,如果未能解决你的问题,请参考以下文章

sh (((bash shell script)))1。找到给定整数的素数因子,2。打印一个模式,3。告诉一个整数可以被11整除或不整除

sh Bash Shell:找出变量是否为空 - 来自https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is

Linux08--Shell程序设计03 shell script

Linux shell 脚本

如何在不使用“sh”或“bash”命令的情况下运行 shell 脚本?

如何让shell脚本变成可执行文件