素数是只有两个因子的数,即数本身和1。例如,2、3、5、7、11、13、17、19、23、29等。
在这里,我们给定一个数,需要判断给定的数是否为素数。
input : a numberoutput : “the number is prime ” or “the number is not prime” based on the number.
示例−
input : 23output : the number is prime
算法步骤 1 - 从2到n/2循环,i作为循环变量
步骤 2 - 如果数字可被整除,打印“该数字不是质数”并设置标志为1;
步骤 3 - 如果标志不等于1,则打印“该数字是质数”。
步骤 4 - 退出。
程序number=53i=2flag=0while test $i -le `expr $number / 2`doif test `expr $number % $i` -eq 0thenflag=1fii=`expr $i + 1`done if test $flag -eq 1thenecho "the number is not prime"elseecho "the number is prime"fi
输出the number is prime
以上就是检查一个数字是否为质数的bash程序的详细内容。
