Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
784 views
in Technique[技术] by (71.8m points)

big o - What is O(log(n!)) and O(n!) and Stirling Approximation

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 Approximation, but I don't get the explanation very well.

Could someone correct me if I'm wrong (about O(log(n!) = O(n log(n)))? And if possible the math in simpler terms? I don't think I will need to prove that in reality I just want an idea of how this works.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...