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

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

In shell scripting how to find factorial of a number?

13条回答
  •  忘掉有多难
    2021-02-14 04:04

    There are a number of instructive examples on Rosetta Code.

    Here's one I found particularly useful:

    function factorial {
      typeset n=$1
      (( n < 2 )) && echo 1 && return
      echo $(( n * $(factorial $((n-1))) ))
    }
    

提交回复
热议问题