递归阶乘程序的复杂性

编程入门 行业动态 更新时间:2024-10-13 00:33:54
本文介绍了递归阶乘程序的复杂性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

查找数字 n 的阶乘的递归程序的复杂性是什么?我的直觉是它可能是 O(n)。

解决方案

如果将乘法作为 O(1),那么可以, O(N)是正确的。但是,请注意,将任意长度 x 的两个数相乘不是 O(1)在有限的硬件上-随着 x 趋于无穷大,乘法所需的时间会增加(例如,如果您使用 Karatsuba乘法,它是 O(x ** 1.585))。

从理论上讲,您可以使用Schönhage-Strassen,但我承认我对此没有任何实际经验。 x ,长度或数字位数(无论以何种底数,对于N的big-O都无关紧要,随一起增长当然,O(log N)。

如果您想将问题限制为足够短的乘数乘以 O(1),那么 N 不可能趋于无穷大,因此big-O表示法是不合适的。 / p>

What's the complexity of a recursive program to find factorial of a number n? My hunch is that it might be O(n).

解决方案

If you take multiplication as O(1), then yes, O(N) is correct. However, note that multiplying two numbers of arbitrary length x is not O(1) on finite hardware -- as x tends to infinity, the time needed for multiplication grows (e.g. if you use Karatsuba multiplication, it's O(x ** 1.585)).

You can theoretically do better for sufficiently huge numbers with Schönhage-Strassen, but I confess I have no real world experience with that one. x, the "length" or "number of digits" (in whatever base, doesn't matter for big-O anyway of N, grows with O(log N), of course.

If you mean to limit your question to factorials of numbers short enough to be multiplied in O(1), then there's no way N can "tend to infinity" and therefore big-O notation is inappropriate.

更多推荐

递归阶乘程序的复杂性

本文发布于:2023-11-29 22:35:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647823.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   阶乘   复杂性   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!