Ruby 阶乘函数
我要疯了:Ruby 的阶乘函数在哪里?不,我不需要教程实现,我只想要库中的函数。这不是数学!
我开始怀疑,这是标准库函数吗?
I'm going crazy: Where is the Ruby function for factorial? No, I don't need tutorial implementations, I just want the function from the library. It's not in Math!
I'm starting to doubt, is it a standard library function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
标准库中没有阶乘函数。
There is no factorial function in the standard library.
像这样就更好了
Like this is better
它不在标准库中,但您可以扩展 Integer 类。
NB 出于明显的性能原因,迭代阶乘是更好的选择。
It's not in the standard library but you can extend the Integer class.
N.B. Iterative factorial is a better choice for obvious performance reasons.
无耻地抄袭自 http://rosettacode.org/wiki/Factorial#Ruby,我个人最喜欢的是
该实现也恰好是 Rosetta 代码中列出的变体中最快的。
更新 #1
添加了
|| 1
处理零情况。更新 #2
感谢和赞赏 Mark Thomas,这里有一个更高效、更优雅、更晦涩的版本:
Shamelessly cribbed from http://rosettacode.org/wiki/Factorial#Ruby, my personal favorite is
This implementation also happens to be the fastest among the variants listed in Rosetta Code.
update #1
Added
|| 1
to handle the zero case.update #2
With thanks and appreciation to Mark Thomas, here's a version that is a bit more efficient, elegant and obscure:
在数学中,
n 的阶乘
只是 n+1 的gamma 函数
(参见:http://en.wikipedia.org/wiki/Gamma_function)
有
Math.gamma()
因此只需使用Math.gamma(n+1)
并将其转换回整数(如果需要)。In math,
factorial of n
is just thegamma function of n+1
(see: http://en.wikipedia.org/wiki/Gamma_function)
Ruby has
Math.gamma()
so just useMath.gamma(n+1)
and cast it back to an integer if desired.您还可以使用
Math.gamma
函数可归结为整数参数的阶乘。You could also use
Math.gamma
function which boils down to factorial for integer parameters.例子
examples
我会做
I would do
我刚刚写了我自己的:
另外,你可以定义一个下降阶乘:
I just wrote my own:
Also, you can define a falling factorial:
怀着对所有参与并花时间帮助我们的人的崇高敬意,我想分享我对此处列出的解决方案的基准。
参数:
迭代次数 = 1000
n = 6
对于 n = 10
With high respect to all who participated and spent their time to help us, I would like to share my benchmarks of the solutions listed here.
Params:
iterations = 1000
n = 6
For n = 10
只需调用此函数
示例即可
Just call this function
examples
使用 Math.gamma.floor 是一种简单的方法来生成近似值,然后将其向下舍入为正确的整数结果。应该适用于所有整数,如有必要,请包括输入检查。
Using
Math.gamma.floor
is an easy way to produce an approximation and then round it back down to the correct integer result. Should work for all Integers, include an input check if necessary.这只是另一种方法,尽管实际上没有必要。
Just another way to do it, although it really isn't necessary.
您可能会发现 Ruby 功能请求很有用。它包含一个重要的 补丁,其中包含 演示 Bash 脚本。简单循环与批次中提供的解决方案之间的速度差异实际上可以是 100 倍(数百倍)。全部用纯 Ruby 编写。
You will probably find a Ruby feature request useful. It contains a nontrivial patch that includes a demo Bash script. The speed difference between a naive loop and the solution presented in the batch can be literally 100x (hundred fold). Written all in pure Ruby.
这是我的版本,尽管它不那么干净,但对我来说似乎很清楚。
这是我的 IRB 测试线,显示了每个步骤。
Here is my version seems to be clear to me even though it's not as clean.
This was my irb testing line that showed each step.
当有一个用于此确切目的的内置迭代器时,为什么标准库需要阶乘方法?它被称为
upto
。不,您不需要使用递归,就像所有其他答案所示。
相反,内置迭代器 upto 可用于计算阶乘:
Why would the standard library require a factorial method, when there is a built-in iterator for this exact purpose? It is called
upto
.No you do not need to use recursion, like all these other answers show.
Rather, the built-in iterator upto can be used to calculate factorials:
还有另一种方式(=
And yet another way (=
还有一种方法可以做到这一点:
Just one more way to do it:
在 Ruby 中,阶乘的标准库函数不可用。我们可以用这种方式在 ruby 中创建一个简单的阶乘函数。
In Ruby standard library function for factorial is not available. We can make a simple function of factorial in ruby in this way.