Recursive Fibonacci in Bash script

前端 未结 5 1379
名媛妹妹
名媛妹妹 2020-12-20 06:44

This is my attempt:

#!/bin/bash

function fibonacci(){

first=$1
second=$2

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

else 

return $(fibonacci $((first-1))         


        
5条回答
  •  渐次进展
    2020-12-20 07:23

    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

提交回复
热议问题