【问题标题】:Shell script to display the perfect numbers in the given range of numbers用于显示给定数字范围内的完美数字的 Shell 脚本
【发布时间】:2021-03-30 22:24:29
【问题描述】:

我正在尝试解决这个问题,并且我知道如何检查一个完美的数字。其中的代码是

read no
i=1
ans=0
while  [  $i  -le  `expr  $no  /  2` ]
do
if  [  `expr  $no  %  $i`  -eq  0  ]
then
ans=`expr  $ans  +  $i`
fi
i=`expr  $i  +  1`
done
if  [  $no  -eq  $ans ]
then  echo  $no is perfect
else
echo  $no is not perfect
fi

我无法理解如何输入数字范围并进行检查。我相信使用 until 循环会有所帮助,但我无法理解如何?

【问题讨论】:

    标签: shell


    【解决方案1】:

    您需要一个for 循环,如https://www.gnu.org/software/bash/manual/bash.html#Looping-Constructs 中所述

    for ((no = 1; no < $upper_limit; no++)); do
        ...
    done
    

    【讨论】:

      猜你喜欢
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      相关资源
      最近更新 更多