你如何在 Bash 脚本中找到一个数字的阶乘?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3394580/
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-09 19:32:30  来源:igfitidea点击:

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

bash

提问by user409345

In shell scripting how to find factorial of a number?

在shell脚本中如何找到一个数字的阶乘?

回答by ghostdog74

seq -s "*" 1 500 |bc

回答by paxdiablo

You don'tdo it in bash. Intelligent people don't try to cut down trees with a fish, so my advice is to try and use the right tool for the job.

不要bash. 聪明的人不会试图用鱼砍树,所以我的建议是尝试使用正确的工具来完成这项工作。

You can use, for example, bcto do it thus:

例如,您可以使用以下bc方法执行此操作:

pax> echo 'define f(x) {if (x>1){return x*f(x-1)};return 1}
           f(6)' | bc
720
pax> echo 'define f(x) {if (x>1){return x*f(x-1)};return 1}
           f(500)' | BC_LINE_LENGTH=99999 bc
12201368259911100687012387854230469262535743428031928421924135883858
45373153881997605496447502203281863013616477148203584163378722078177
20048078520515932928547790757193933060377296085908627042917454788242
49127263443056701732707694610628023104526442188787894657547771498634
94367781037644274033827365397471386477878495438489595537537990423241
06127132698432774571554630997720278101456108118837370953101635632443
29870295638966289116589747695720879269288712817800702651745077684107
19624390394322536422605234945850129918571501248706961568141625359056
69342381300885624924689156412677565448188650659384795177536089400574
52389403357984763639449053130623237490664450488246650759467358620746
37925184200459369692981022263971952597190945217823331756934581508552
33282076282002340262690789834245171200620771464097945611612762914595
12372299133401695523638509428855920187274337951730145863575708283557
80158735432768888680120399882384702151467605445407663535984174430480
12893831389688163948746965881750450692636533817505547812864000000000
00000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000

回答by Vitalii Fedorenko

#!/bin/bash
counter= #first argument
factorial=1
while [ $counter -gt 0 ] #while counter > 0
do
   factorial=$(( $factorial * $counter ))
   counter=$(( $counter - 1 ))
done
echo $factorial

回答by nos

echo 500 | dc -e '?[q]sQ[d1=Qd1-lFx*]dsFxp'

回答by Nordic Mainframe

10! in bash:

10!在 bash 中:

f=1; for k in {1..10}; do f=$[$k * $f] ; done; echo $f

or here in a step by step fashion:

或者在这里一步一步地:

$ t=$(echo {1..10})
$ echo $t
1 2 3 4 5 6 7 8 9 10
$ t=${t// /*}
$ echo $t
1*2*3*4*5*6*7*8*9*10
$ echo $[$t]
3628800

回答by HuZaiFa ZahoOr

echo Enter Number

read num
fact=1
for ((i=1;i<=num;i++))
do
fact=$(($fact*$i))
done
echo $fact

回答by Paused until further notice.

Here is a recursive function in Bash:

这是 Bash 中的递归函数:

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

Of course it's quite slow and limited.

当然,它非常缓慢且有限。

回答by user456584

There are a number of instructive examples on Rosetta Code.

关于 Rosetta Code有许多有启发性的例子。

Here's one I found particularly useful:

这是我发现特别有用的一个:

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

回答by Milen John Thomas

Please use this script to find factorial in Bash,

请使用此脚本在 Bash 中查找阶乘,

#!/bin/bash

num=
fact=1
for((i=1; i<=$num; i++))
do
        let fact=fact*i
done

echo "Factorial is $fact"

回答by insanikov

I almost completely agree with Vitalii Fedorenko, I would just like paxdiablo suggests use bc, here's the code from Vitalii Fedorenko but modified to use bc.

我几乎完全同意 Vitalii Fedorenko,我只是希望 paxdiablo 建议使用bc,这是来自 Vitalii Fedorenko 的代码,但修改为使用bc.

#!/bin/bash
counter=
output=1
while [ $counter -gt 1 ] #while counter > 1 (x*1=x)
do
        output=$(echo "$output * $counter" | bc)
        counter=$(($counter - 1))
done
#remove newlines and '\' from output
output=$(echo "$output" | tr -d '\' | tr -d '\n')
echo "$output"
exit

This method is better because bcallows you to use strings, instead of integers, making it possible for you to calculate much bigger numbers.

这种方法更好,因为bc允许您使用字符串而不是整数,使您可以计算更大的数字。

I apologize if I haven't used trcorrectly, I am not very familiar with it.

如果我没有tr正确使用,我很抱歉,我对它不是很熟悉。