Shell小技巧(五十五)使用高斯算法计算累加之和

高斯算法如下:

#/bin/bash

source ./afunction.sh

if [ -z $1 ]; then

  echo "please input number !Now will be exit!"

  exit 1

fi

x=$1

IsNumber $x

r=$?

if [ $r -gt 0 ]; then

  echo "please input number !Now will be exit!!"

  exit 1

fi

is2 $x

y=$?

if [ $y -eq 0 ];then

   m=0

   k=0

else

   m=1

   k=x

fi

let n=(x-m)/2

let sum=(x-m+1)*n+k

echo $x

echo $n

echo "Result : " $sum

发布了95 篇原创文章 · 获赞 3 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/bigwood99/article/details/105626354