shell求质数,很多人面试的时候会遇到
- #!/bin/sh
- for((i=10000;i<=12000;i++))
- do
- for((k=2;k<=i;k++))
- do
- if [ $k -eq $i ]
- then
- echo "$i su shu"
- break
- fi
- if [ $(($i%k)) -eq 0 ]
- then break
- fi
- done
- done
- #!/bin/bash
- i=19000
- N=20000
- while [ "$i" -lt "$N" ]
- do
- flag=1
- j=2
- k=$(echo "sqrt($i)"|bc)
- while [ "$j" -le "$k" ]
- do
- let "m = $i % $j"
- if [ "$m" -eq 0 ]
- then
- flag=0
- fi
- let "j = $j + 1"
- done
- if [ "$flag" -eq 1 ]
- then
- echo $i
- fi
- let "i = $i + 1"
- done
转载于:https://blog.51cto.com/zhouxuguang/566488