How do you find the factorial of a number in a Bash script?

前端 未结 13 2116
别那么骄傲
别那么骄傲 2021-02-14 03:24

In shell scripting how to find factorial of a number?

13条回答
  •  不知归路
    2021-02-14 03:43

    Here is a recursive function in Bash:

    factorial () { 
        if (($1 == 1))
        then
            echo 1
            return
        else
            echo $(( $( factorial $(($1 - 1)) ) * $1 ))
        fi
    }
    

    Of course it's quite slow and limited.

提交回复
热议问题