bash中的递归fibonacci函数

bash中的递归fibonacci函数,bash,recursion,fibonacci,Bash,Recursion,Fibonacci,我仔细阅读了这篇文章,想从中获得一些代码方面的帮助,并遇到了一个大约4个月前的帖子,但是用户的最终修订版在发布到bash时无法工作,并产生了一些奇怪的结果。这是我的版本,它也产生了一些奇怪的结果: #!/bin/bash fib() { ind=$1 if (( ind <= 0 )) then echo 0 elif (( ind = 1 )) then echo 1 else echo $(( $(fib $((ind - 1)) ) + $(fib $((ind - 2))

我仔细阅读了这篇文章,想从中获得一些代码方面的帮助,并遇到了一个大约4个月前的帖子,但是用户的最终修订版在发布到bash时无法工作,并产生了一些奇怪的结果。这是我的版本,它也产生了一些奇怪的结果:

#!/bin/bash
fib()
{
ind=$1

if (( ind <= 0 ))
 then echo 0
elif (( ind = 1 ))
 then echo 1
else
  echo $(( $(fib $((ind - 1)) ) + $(fib $((ind - 2)) ) )) 
fi
}
echo fibbonacci sequence number $1 is $(fib $1)
#/bin/bash
fib()
{
ind=$1

if((ind通常在编写斐波那契序列逻辑时,必须对前两个数字进行特殊的大小写。这就是第一个用户所做的:特殊大小写0和1

您删除了一个特殊大小写的实例,并将所有内容移动了一个索引,这解释了一个移动。另一个很简单:代码是零索引的。这就是为什么所有内容都“减少了两个”

原始代码有什么问题?这一行:

elif ((ind = 1))
将ind设置为1。否则就可以了

代码的一个简单修复方法是替换此行:

if (( ind <= 0 ))
我应该使用double=,并将1改为2,所以应该是

elif (( ind == 2 )) 
最终,我正确的脚本应该是这样的

#!/bin/bash
#fibonacci sequence function
fib()
{
ind=$1

if (( ind <= 1 ))
 then echo 0
elif (( ind == 2 ))
 then echo 1
else
  echo $(( $(fib $((ind - 1)) ) + $(fib $((ind - 2)) ) )) 
fi
 }
echo fibbonacci sequence number $1 is $(fib $1)
!/bin/bash
#斐波那契序列函数
fib()
{
ind=$1

如果((ind足够有趣,Korn shell执行@christianternus'脚本的速度比其他shell快得多:

$ for a in sh bash zsh ksh; do echo "shell: $a"; time for i in $(seq 1 20); do $a bin/fib.sh $i; done | md5sum; done shell: sh 5fece53a38f2df040bfaf9632c2b7f4b - real 0m29.508s user 0m3.788s sys 0m11.785s shell: bash 5fece53a38f2df040bfaf9632c2b7f4b - real 0m29.906s user 0m3.604s sys 0m11.235s shell: zsh 5fece53a38f2df040bfaf9632c2b7f4b - real 0m29.203s user 0m2.505s sys 0m14.377s shell: ksh 5fece53a38f2df040bfaf9632c2b7f4b - real 0m0.942s user 0m0.843s sys 0m0.079s sh bash zsh ksh中的$a;do echo“shell:$a”;i的时间为$(序号120);do$a bin/fib.sh$i;完成| md5sum;完成 贝壳:上海 5fece53a38f2df040bfaf9632c2b7f4b- 实际0m29.508s 用户0m3.788s 系统0m11.785s shell:bash 5fece53a38f2df040bfaf9632c2b7f4b- 实际0m29.906s 用户0m3.604s 系统0m11.235s 外壳:zsh 5fece53a38f2df040bfaf9632c2b7f4b- 实际0m29.203s 用户0m2.505s 系统0m14.377s 外壳:ksh 5fece53a38f2df040bfaf9632c2b7f4b- 实际0.942s 用户0.843s 系统0m0.079s
你有什么bash版本(
bash--version
)。在
GNUBash上输入你的代码,版本4.2.8(2)-release(x86_64-apple-darwin11)
工作得很好。OP:我不知道第三个fibbonaci数是1。请告诉我。所以elif((ind=1))将ind设置为1?我不明白,当我替换if时,我以为那是else if块的语法((ind几乎!你想要
==
进行相等性检查,而不是
=
。好的,我设法让脚本正常工作。非常感谢!没问题!确保接受答案(单击复选标记)如果这对你有帮助的话。当然,bash中的这个实现是高度次优的(练习:计算fib函数被调用的次数)!一个好的练习是实现记忆化。简单的bash数组非常容易,而且非常有趣!请评论你的代码。它对我不起作用。不知道为什么
#!/bin/bash
#fibonacci sequence function
fib()
{
ind=$1

if (( ind <= 0 ))
 then echo 0
elif (( ind == 2 ))
 then echo 1
else
  echo $(( $(fib $((ind - 1)) ) + $(fib $((ind - 2)) ) )) 
fi 
}
echo fibbonacci sequence number $1 is $(fib $1)
elif (( ind = 1 ))
elif (( ind == 2 )) 
#!/bin/bash
#fibonacci sequence function
fib()
{
ind=$1

if (( ind <= 1 ))
 then echo 0
elif (( ind == 2 ))
 then echo 1
else
  echo $(( $(fib $((ind - 1)) ) + $(fib $((ind - 2)) ) )) 
fi
 }
echo fibbonacci sequence number $1 is $(fib $1)
$ for a in sh bash zsh ksh; do echo "shell: $a"; time for i in $(seq 1 20); do $a bin/fib.sh $i; done | md5sum; done shell: sh 5fece53a38f2df040bfaf9632c2b7f4b - real 0m29.508s user 0m3.788s sys 0m11.785s shell: bash 5fece53a38f2df040bfaf9632c2b7f4b - real 0m29.906s user 0m3.604s sys 0m11.235s shell: zsh 5fece53a38f2df040bfaf9632c2b7f4b - real 0m29.203s user 0m2.505s sys 0m14.377s shell: ksh 5fece53a38f2df040bfaf9632c2b7f4b - real 0m0.942s user 0m0.843s sys 0m0.079s
#!/bin/bash

function fib(){
    if [ $1 -le 0 ]; then
        echo 0
    elif [ $1 -eq 1 ]; then
        echo 1
    else
        echo $[`fib $[$1-2]` + `fib $[$1 - 1]` ]
    fi

}

fib $1