如何使用库调用计算 C# 中的阶乘?
我需要计算 100 左右的数字的阶乘!为了确定一系列硬币翻转式数据是否是随机的,按照这个维基百科条目贝叶斯概率。 正如您所看到的,必要的公式涉及 3 个阶乘计算(但有趣的是,其中两个阶乘计算是在第三个阶乘计算的过程中计算的)。
我在这里看到了这个问题,但我认为整数是很快就会被吹灭。我还可以创建一个关于阶乘计算更智能的函数(即,如果我有 11!/(7!3!),按照 wiki 示例,我可以转到(11 * 10 * 9 * 8)/ 3!),但这对我来说有点过早优化的味道,因为我希望它能够工作,但我不关心速度(还)。
那么,我可以调用什么好的 C# 库来计算阶乘以获得该概率呢?我对阶乘计算中的所有神奇功能不感兴趣,我只想以一种我可以操纵它的方式得到结果。 Math 命名空间中似乎没有阶乘函数,因此出现了问题。
I need to calculate the factorial of numbers up to around 100! in order to determine if a series of coin flip-style data is random, as per this Wikipedia entry on Bayesian probability. As you can see there, the necessary formula involves 3 factorial calculations (but, interestingly, two of those factorial calculations are calculated along the way to the third).
I saw this question here, but I'd think that integer is going to get blown out pretty quickly. I could also make a function that is more intelligent about the factorial calculation (ie, if I have 11!/(7!3!), as per the wiki example, I could go to (11*10*9*8)/3!), but that smacks of premature optimization to me, in the sense that I want it to work, but I don't care about speed (yet).
So what's a good C# library I can call to calculate the factorial in order to get that probability? I'm not interested in all the awesomeness that can go into factorial calculation, I just want the result in a way that I can manipulate it. There does not appear to be a factorial function in the Math namespace, hence the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您想计算阶乘或二项式系数吗?
听起来您想计算二项式系数 - 特别是当您提到 11!/(7!3!) 时。
可能有一个库可以为您执行此操作,但作为一名(大概)访问堆栈溢出的程序员,没有理由不自己编写一个库。这并不太复杂。
为了避免内存溢出,在删除所有公共因素之前不要评估结果。
这个算法仍然需要改进,但是你在这里已经有了良好算法的基础。为了获得最佳结果,分母值需要分解为其质因数。就目前情况而言,这将很快运行 n = 50。
我可以估计 n = 110,k = 50(返回 6x10^31),但无法运行 n = 120,k = 50。
Do you want to calculate factorials, or binomial coefficients?
It sounds like you want to calculate binomial coefficients - especially as you mention 11!/(7!3!).
There may be a library that can do this for you, but as a (presumably) programmer visiting stack overflow there's no reason not to write one yourself. It's not too complicated.
To avoid memory overflow, don't evaluate the result until all common factors are removed.
This algorithm still needs to be improved, but you have the basis for a good algorithm here. The denominator values need to be split into their prime factors for the best result. As it stands, this will run for n = 50 quite quickly.
I can estimate n = 110, k = 50 (returns 6x10^31) but cannot run n = 120, k = 50.
下面可以在1秒内计算出5000的阶乘。
The following can calculate the factorial of 5000 in 1 second.
大家好,根据这个解决方案,我有自己的解决方案,我计算数组一维元素的阶乘。代码是 `int[] array = new int[5]
{
4,3,4,3,8
};
将上面的代码复制并粘贴到按钮中,它可以求解一维数组元素的阶乘。此致。
hello everybody according to this solution i have my own solution where i calculate factorial of array 1D elements. the code is `int[] array = new int[5]
{
4,3,4,3,8
};
copy and paste the code above ^ in the button , it solves factorial of elements of array 1D. best regards.
您可以尝试 Math.NET - 我没有使用过该库,但他们确实列出了阶乘和对数阶乘。
You could try Math.NET - I haven't used that library, but they do list Factorial and Logarithmic Factorial.
上一个问题涉及类似的主题。那里有人链接了快速阶乘函数网站,其中包括一些高效的解释算法甚至 C# 源代码。
There has been a previous question on a similar topic. Someone there linked the Fast Factorial Functions web site, which includes some explanations of efficient algorithms and even C# source code.