You are not logged in. You can browse but not post. Login or Register by clicking 'Login or Register' at the top-right of this page. For more information on Statalist, see the FAQ.
John Mullahy That's a good question. Stata is written for its users, but Stata developers can use it too, while Mata was written for Stata developers, but users can use it too. That is a little sardonic but I think may be related to the question. Quite a few functions in Mata are there because the developers really needed them. Here "Stata developers" is jargon for those StataCorp people who write the official code.
factorial gets big really really really fast, so big that it becomes very quickly impossible to hold that number in a meaningful way. For example, 167! = 1.5036 * 10^300 (167 is the largest number for which Mata will compute the factorial). This is not so much a problem for the logarithm of the factorial, so that is what you should be using instead.
---------------------------------
Maarten L. Buis
University of Konstanz
Department of history and sociology
box 40
78457 Konstanz
Germany http://www.maartenbuis.nl
---------------------------------
To the excellent advice given by others, I will just add a question: why do you want to compute n! ? It's a legitimate thing to want to do, but if your ultimate purpose is just to compute the number of combinations of n things taken r at a time, Stata already has a -comb()- function that will do that for you. Everybody's workflow is different, but in mine, I use -comb()- somewhat regularly, whereas I have seldom needed to compute a factorial for its own sake.
Just to add a bit to #6, its difficult to overstate just how fast a factorial grows. Factorial growth is faster than exponential growth. Theoretically, if you could fold a sheet of paper 42 times you would have a folded piece of paper so thick it would reach the moon. That is exponential growth at work and factorial growth is even faster. Of course, that's not to say the algorithm processing time grows factorially. The naïve algorithm is in linear time with respect to the value (not the size) of the input and it turns out you can do even better than the naïve algorithm. Factorial calculations do present a memory problem because the factorial of relatively small numbers can easily exceed your storage capacity for a number.
Comment