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

the first C version looks the best to me, it is extremely clear and concise. For a slight improvement, declare the variable c inside the loop.

Yet, since 20! is the last factorial representable in a u64, there is not much a point for these functions, and you should definitely check for n<21. It would be more elegant to store a lookup table with the 21 possible results.

In practice you would want the logarithm of the factorial, that is computed by the "lgamma" function from the C standard math.h. Is such a thing available in rust?

Edit: if you use doubles (which is more reasonable for that use case), you can also do that:

     double factorial(double n) { return tgamma(1+n); }


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

Search: