Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complexity of recursive factorial program

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

like image 483
Karan Avatar asked Feb 24 '10 15:02

Karan


People also ask

How do you find the complexity of a recursive program?

Method 1: Recursion Tree Method We take the sum of each value of nodes to find the total complexity of the algorithm. Draw a recursion tree based on the given recurrence relation. Determine the number of levels, cost at each level and cost of the last level. Add the cost of all levels and simplify the expression.

What is factorial complexity?

Recall that a factorial is the product of the sequence of n integers. For example, the factorial of 5, or 5!, is: 5 * 4 * 3 * 2 * 1 = 120. We will find ourselves writing algorithms with factorial time complexity when calculating permutations and combinations.

What is time and space complexity of iterative factorial function?

For the time complexity of the iterative solution, you have n multiplications in the for loop, so a loose bound will be O(n) . Every other operation can be assumed to be unit time or constant time, with no bearing on the overall efficiency of the algorithm.

What is the space complexity of the above recursive implementation to find the factorial of a number?

Explanation: The space complexity of the above recursive implementation to find the factorial of a number is O(1).


2 Answers

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.

like image 106
Alex Martelli Avatar answered Oct 06 '22 01:10

Alex Martelli


Assuming you're talking about the most naive factorial algorithm ever:

factorial (n):   if (n = 0) then return 1   otherwise return n * factorial(n-1) 

Yes, the algorithm is linear, running in O(n) time. This is the case because it executes once every time it decrements the value n, and it decrements the value n until it reaches 0, meaning the function is called recursively n times. This is assuming, of course, that both decrementation and multiplication are constant operations.

Of course, if you implement factorial some other way (for example, using addition recursively instead of multiplication), you can end up with a much more time-complex algorithm. I wouldn't advise using such an algorithm, though.

like image 38
Welbog Avatar answered Oct 06 '22 01:10

Welbog