Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What's the advantage of this approximation in python/code? I can see it being useful analytically, but what's the advantage in code? The formula has an ^n in it, so doesn't it have the same complexity as n! ?


No, because you don't compute n^n by multiplying n by n, n times. Your also don't compute a x b by summing b, a times, do you?

You compute n^n the same way you compute a^n which is to say that a^n = e^(n x log(a))


There are efficient ways to compute x^n. Eg. you want to compute x^8. Instead of multiplying x eight times by itself, you can do ((x^2)^2)^2, which is just three multiplications.

To be fair, I’m not sure whether similar optimizations exist for computing the factorial, but I don’t think so.


there aren't similar tricks for factorials.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: