计算阶乘的快速算法
我发现 FastFactorialFunctions 描述了许多计算阶乘的算法。不幸的是,这些解释很简洁,我不想通过一行又一行的源代码来理解算法背后的基本原理。
有人可以向我指出这些(或其他快速)算法的更详细描述,用于快速计算大型精确阶乘吗?
质因数分解的阶乘 (Python)< /a> 描述了素因式分解的方法,这是所有性能最佳的因式分解算法所共有的技术。它还包含一些很好的 Python 示例代码。作者链接到二进制分割的描述并引用了一篇文章算法杂志(“计算阶乘的复杂性”)看起来很有前途,如果我能得到我的动手。
I found FastFactorialFunctions describing a number of algorithms for computing the factorial. Unfortunately, the explanations are terse and I don't feel like sifting through line after line of source code to understand the basic principles behind the algorithms.
Can anybody point me to more detailed descriptions of these (or other fast) algorithms for computing large exact factorials fast?
Factorials with prime factorization (Python)
describes the method of prime factorization, the technique common to all of the best-performing factorial algorithms. It also contains some nice example code in Python. The author links to a description of binary splitting and references an article in the Journal of Algorithms ("On the Complexity of Calculating Factorials") that looks promising, if I could only get my hands on it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看 Richard Fateman 撰写的这篇论文(PDF 链接)。代码示例是用 Lisp 编写的,但无论如何,大部分秘密都归结为最大限度地减少必须执行的 bignum(任意精度整数)计算的数量。
当然,如果你不需要/没有 bignum,那就很简单了;查找表或简单的循环都可以。
编辑:如果您可以使用近似答案,则可以通过对
k = 2 ...的
,或者使用古老的斯特林近似。您希望尽可能使用对数以避免溢出;特别是,斯特林近似的天真的应用会在很多不需要的地方溢出。log(k)
求和来直接计算阶乘的对数... nCheck out this paper (PDF link) by Richard Fateman. The code samples are in Lisp, in but in any event, much of the secret boils down to minimizing the number of bignum (arbitrary precision integer) calculations you have to do.
Naturally, if you don't need/have bignums, it's trivial; either a lookup table or a simple loop will be fine.
EDIT: If you can use an approximate answer, you can either compute the logarithm of the factorial directly by summing
log(k)
fork = 2 ... n
, or by using the venerable Stirling approximation. You want to work with the logarithm wherever possible to avoid overflow; in particular, a naive application of Stirling's approximation will overflow in a lot of places where it doesn't have to.还有另一种方法。 此处详细介绍了此方法,该方法将乘法量减半以进行少量加法和减法。您可能想要显示的第一种方法,而显示的第二种方法如果您能理解的话,读起来很有趣。
There is also another method. This method is detailed here that halves the amount of multiplication for a little bit of addition and subtraction. You probably want the first method shown, and the second method shown is an interesting read if you can understand it.
使用并行化。例如,在 Java 中:
规范:
第 11 代 Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
RAM 16,0 GB
64 位操作系统。
使用 Eclipse 在 Windows 11 中进行测试。
Use parallelization. For example, in Java:
Specifications:
11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
RAM 16,0 GB
64 bits operating system.
Tested in Windows 11 using Eclipse.
十多年后,我想提供一种 Python 方法,灵感来自于您对乘法
factorial(n) * n+1
感兴趣,并且基本情况是0< /code> 和
1
其结果为1
,那么:对于 100 000 的阶乘,在我的机器上最多需要 5 秒,我希望它可以为文档和即将到来的查看者提供服务!
诗。同样的想法对于计算斐波那契很有用,斐波那契是求和而不是乘法。
More than ten years later, I would like to provide a Python approach inspired in the fact that you're interested in multiply
factorial(n) * n+1
and the base cases are0
and1
whose result is1
, then:For factorial of 100 000 it takes up to 5 seconds in my machine, I hope it serves for documentation and upcoming viewers!
Ps. Same idea is useful to compute fibonacci, which is a summation not a multiplication.