Computing the factorial n! in SPSS Statistics
Can SPSS Statistics compute factorials? That is,
n! = n * (n-1) * (n-2) * ... * 3 * 2 * 1
Resolving the problem
Yes, using the LNGAMMA function. This computes the natural logarithm of the gamma function. Since
n! = gamma(n+1)
the following SPSS command syntax will compute the factorial of n:
IF (n >= 0 and n=rnd(n)) nfact = RND(EXP(LNGAMMA(n+1))).
Note that although n! is only defined for non-negative integers, the gamma function implemented in SPSS Statistics is a generalization, defined for all positive real numbers. The IF condition is used to insure that the factorial is only calculated when n is nonnegative (n>= 0) and an integer (n=rnd(n), i.e., n equals the rounded value of n).
If using dialog boxes, click on Transform>Compute in the Data Editor window. Enter a new variable name, such as nfact, as the Target Variable. Then build the Numeric Expression by choosing first RND, then EXP, then LNGAMMA from the list of functions, pasting each function in turn into the Numeric Expression box, Then paste in the variable for which you want the factorial calculated. With the cursor still in the innermost parentheses and just following the variable name, click the plus sign and the digit 1 in the keypad.
To implement the condition for the calculation, click the If button at the bottom of the dialog. In the "If cases" dialog, click the radio button beside "If case satisfies condition". Enter the conditional phrase, "(n >= 0 and n=rnd(n))" and click Continue. In the main Compute Variable dialog, click OK to run the computation or Paste to paste the above command into a syntax window.
Translate this page: