您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

检查一个数字是否为质数的Bash程序

2024/3/11 13:48:02发布18次查看
bash(也称为gnu bash)是一种命令语言和unix shell脚本,是一种用于操作系统的命令行解释器。它由brian fox设计,是一种替代bourne shell的自由软件。它首次发布于1989年,并成为类似macos、基于linux的操作系统以及其他基于linux的软件的登录shell的首选。
素数是只有两个因子的数,即数本身和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程序的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product