Linux下用shell编写斐波那契数列前十项之和

写出斐波那契的前十项和,代码如下:

#!/bin/bash
a=1
b=1
echo -n -e "$a\t$b"
         let "n=a+b"
         count=4
while [ $count -gt 0 ]
do
         let "a=a+b"
         let "b=b+a"
echo -n -e "\t$a\t$b"
         let "n+=a+b"
         let "count=count-1"
done
echo
echo "The sum is $n"
#end

运行结果:

[root@localhost dajun]# bash c
1	1	2	3	5	8	13	21	34	55
The sum is 143

然后又发现了一种简单的(主要是方便记忆)

#!/bin/bash
a=1
b=1
c=0
s=0
for((i=0; i<10; ++i));do
echo "$a"
let s+=a
let c=a+b
let a=b
let b=c
done
echo "sum=$s"

运行结果同上;

你可能感兴趣的:(shell)