Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to calculate n!

    I wonder how to calculate n!=1x2x3x4....x n?

    Thanks!

  • #2
    Stata has a function for the natural log of the factorial.

    Example

    Code:
    di exp(lnfactorial(5))

    Comment


    • #3
      I have often wondered why there is a factorial function in Mata but not in Stata.
      Code:
      . mata factorial(6)
        720
      
      . di factorial(6)
      unknown function factorial()
      r(133);

      Comment


      • #4
        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.

        Comment


        • #5
          Re: #3 and #4, -factorial- is not alone in this regard:
          Code:
          . mata gamma(6)
            120
          
          . di gamma(6)
          unknown function gamma()
          r(133);
          Maybe there are other instances as well?

          Comment


          • #6
            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
            ---------------------------------

            Comment


            • #7
              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.

              Comment


              • #8
                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

                Working...
                X