#/usr/bin/sh
n=$1
sum=0
# 方法1 循环
for i in $(seq 1 $n); do
((sum = $sum + $i))
done
echo $sum
# 方法2 公式
((sum=(1 + $n) * $n / 2))
echo $sum
数列和sum=(首项h+末项t)×项数i÷2
项数n=(末项t-首项h)÷公差s+1
公差s=数列中两个数字之间的步进距离
#!/bin/bash
read -p "Input the head number: " h
read -p "Input the tail number: " t
read -p "Input the step length: " s
sum=(h+t)*((t-h)/s+1)/2
如23456求和
首项h=2
尾项t=6
公差s=1
sum=(2+6)((6-2)/1+1)/2
=8*(4+1)/2
=8*5/2
=20