Bash 脚本中的递归斐波那契数列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17136505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:42:23  来源:igfitidea点击:

Recursive Fibonacci in Bash script

bashfibonacci

提问by rage

This is my attempt:

这是我的尝试:

#!/bin/bash

function fibonacci(){

first=
second=

if (( first <= second ))
then
return 1

else 

return $(fibonacci $((first-1)) ) + $(fibonacci $((second-2)) )
fi
}

echo $(fibonacci 2 0)

I think i'm having trouble with the else statement. I get the error return: +: numeric argument required on line 14.

我想我在使用 else 语句时遇到了问题。我return: +: numeric argument required 在第 14 行收到错误消息。

The other problem that i'm having is that the script doesn't display any numbers even if i do echo $(fibonacci 0 2). I thought it would display a 1 since i'm returning a 1 in that case. Can someone give me a couple of tips on how to accomplish this?

我遇到的另一个问题是,即使我这样做,脚本也不显示任何数字echo $(fibonacci 0 2)。我认为它会显示 1,因为在这种情况下我会返回 1。有人可以给我一些有关如何完成此操作的提示吗?

After checking out some of your answers this is my second attempt.. It works correctly except that it displays the nth fibonacci number in the form 1+1+1+1 etc. Any ideas why?

在检查了你的一些答案之后,这是我的第二次尝试。它工作正常,只是它以 1+1+1+1 等形式显示第 n 个斐波那契数。任何想法为什么?

#!/bin/bash

function fibonacci(){

first=
second=

if (( first <= second ))
then
echo 1


else 

echo $(fibonacci $((first-1)) ) + $(fibonacci $((first-2)) )
fi
}

val=$(fibonacci 3 0)
echo $val

My final attempt:

我的最后尝试:

#!/bin/bash

function fibonacci(){

first=

if (( first <= 0 ))
then
echo 1


else 

echo $(( $(fibonacci $((first-1)) ) + $(fibonacci $((first-2)) ) ))
fi
}

val=$(fibonacci 5)
echo $val

thanks dudes.

谢谢各位。

回答by Andrii Kovalchuk

#!/bin/bash

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

}

fib 

回答by Andrii Kovalchuk

The $(...)substitution operator is replaced with the outputof the command. Your function doesn't produce any output, so $(...)is the empty string.

所述$(...)替换操作符被替换为输出的命令的。您的函数不产生任何输出,$(...)空字符串也是如此。

Return values of a function go into $?just like exit codes of an external command.

函数的返回值$?就像外部命令的退出代码一样。

So you need to either produce some output (make the function echo its result instead of returning it) or use $?after each call to get the value. I'd pick the echo.

因此,您需要生成一些输出(使函数回显其结果而不是返回它)或$?在每次调用后使用以获取值。我会选择回声。

回答by Jester

As Wumpus said you need to produce output using for example echo. However you also need to fix the recursive invocation. The outermost operation would be an addition, that is you want:

正如 Wumpus 所说,您需要使用例如echo. 但是,您还需要修复递归调用。最外面的操作将是一个加法,这是你想要的:

echo $(( a + b ))

Both aand bare substitutions of fibonacci, so

这两个ab是替换fibonacci,所以

echo $(( $(fibonacci x) + $(fibonacci y) ))

xand yare in turn arithmetic expressions, so each needs its own $(( )), giving:

xy又是算术表达式,所以每个都需要自己的$(( )),给出:

echo $(( $(fibonacci $((first-1)) ) + $(fibonacci $((second-2)) ) ))

If you are confused by this, you should put the components into temporary variables and break down the expression into parts.

如果对此感到困惑,则应将组件放入临时变量并将表达式分解为部分。

As to the actual fibonacci, it's not clear why you are passing 2 arguments.

至于实际的斐波那契,不清楚为什么要传递 2 个参数。

回答by niry

Short version, recursive

简短版本,递归

fib(){((<2))&&echo ||echo $(($(fib $((-1)))+$(fib $((-2)))));}

回答by Floyd

While calculating fibonacci numbers with recursion is certainly possible, it has a terrible performance. A real bad example of the use of recursion: For every (sub) fibonacci number, two more fibonacci numbers must be calculated.

虽然使用递归计算斐波那契数当然是可能的,但它的性能却很糟糕。使用递归的一个非常糟糕的例子:对于每个(子)斐波那契数,必须计算另外两个斐波那契数。

A much faster, and simple approach uses iteration, as can be found here:

一种更快、更简单的方法使用迭代,可以在这里找到:

https://stackoverflow.com/a/56136909/1516636

https://stackoverflow.com/a/56136909/1516636