一、输出1到100之间的整数之和
sum=0
i=1
for i in {1..100}
do
sum=$((sum+i))
done
echo "The sum of integers from 1 and 100 is:$sum"
sum=0
for ((i=0;i<=100;i++))
do
sum=$[$sum+$i] #sum=$((sum+i))
done
echo "The sum of integers between 1 and 100 is:$sum"
二、输出1到100之间的偶数之和
sum=0
for ((i=2;i<=100;i+=2))
do
sum=$((sum+i))
done
echo "The sum of even numbers between 1 and 100 is:$sum"
sum=0
for i in {2..100..2}
do
sum=$((sum+i))
done
echo "The sum of odd numbers between 1 and 100:$sum"
三、输出1到100之间奇数之和
sum=0
for ((i=1;i<=100;i+=2))
do
sum=$((sum+i))
done
echo "The sum of odd numbers between 1 and 100 is:$sum"
sum=0
for i in {1..100..2}
do
sum=$((sum+i))
done
echo "The sum of even numbers between 1 and 100:$sum"
四、输出1到100之间的素数
echo "1到100之间的素数为:"
for ((i=2; i<=100; i++))
do
flag=0
for ((j=2; j<i; j++))
do
if [ $((i%j)) -eq 0 ]; then
flag=1
break
fi
done
if [ $flag -eq 0 ]; then
echo "$i " >> sushu.txt
fi
done