Alex Rivera | Logout

What is O(log(n!)), O(n!), and Stirling's approximation?

Asked 2011-11-14T06:51:42.193
62

What is O(log(n!)) and O(n!)? I believe it is O(n log(n)) and O(n^n)? Why?

I think it has to do with Stirling's approximation, but I don't get the explanation very well.

Am I wrong about O(log(n!) = O(n log(n))? How can the math be explained in simpler terms? In reality I just want an idea of how this works.

Edit
Report

1 Answer

115

O(n!) isn't equivalent to O(n^n). It is asymptotically less than O(n^n).

O(log(n!)) is equal to O(n log(n)). Here is one way to prove that:

Note that by using the log rule log(mn) = log(m) + log(n) we can see that:

log(n!) = log(n*(n-1)*...2*1) = log(n) + log(n-1) + ... log(2) + log(1)


Proof that O(log(n!)) ⊆ O(n log(n)):

log(n!) = log(n) + log(n-1) + ... log(2) + log(1)

Which is less than:

log(n) + log(n) + log(n) + log(n) + ... + log(n) = n*log(n)

So O(log(n!)) is a subset of O(n log(n))


Proof that O(n log(n)) ⊆ O(log(n!)):

log(n!) = log(n) + log(n-1) + ... log(2) + log(1)

Which is greater than (the left half of that expression with all (n-x) replaced by n/2:

log(n/2) + log(n/2) + ... + log(n/2) = floor(n/2)*log(floor(n/2)) ∈ O(n log(n))

So O(n log(n)) is a subset of O(log(n!)).


Since O(n log(n)) ⊆ O(log(n!)) ⊆ O(n log(n)), they are equivalent big-Oh classes.

answered 2011-11-14T06:57:38.753

Your Answer