The factorial of a number, written as 



n
!


{\displaystyle n!}

, is defined as 



n
!
=
n
(
n
−
1
)
(
n
−
2
)
.
.
.
(
2
)
(
1
)


{\displaystyle n!=n(n-1)(n-2)...(2)(1)}

.

Multifactorials generalize factorials as follows:

 



n
!
=
n
(
n
−
1
)
(
n
−
2
)
.
.
.
(
2
)
(
1
)


{\displaystyle n!=n(n-1)(n-2)...(2)(1)}


 



n
!
!
=
n
(
n
−
2
)
(
n
−
4
)
.
.
.


{\displaystyle n!!=n(n-2)(n-4)...}


 



n
!
!
!
=
n
(
n
−
3
)
(
n
−
6
)
.
.
.


{\displaystyle n!!!=n(n-3)(n-6)...}


 



n
!
!
!
!
=
n
(
n
−
4
)
(
n
−
8
)
.
.
.


{\displaystyle n!!!!=n(n-4)(n-8)...}


 



n
!
!
!
!
!
=
n
(
n
−
5
)
(
n
−
10
)
.
.
.


{\displaystyle n!!!!!=n(n-5)(n-10)...}


In all cases, the terms in the products are positive integers.

If we define the degree of the multifactorial as the difference in successive terms that are multiplied together for a multifactorial (the number of exclamation marks), then the task is twofold:

 Write a function that given n and the degree, calculates the multifactorial.
 Use the function to generate and display here a table of the first ten members (1 to 10) of the first five degrees of multifactorial.

Note: The wikipedia entry on multifactorials gives a different formula. This task uses the Wolfram mathworld definition.

/*REXX program calculates and displays K-fact (multifactorial) of non-negative integers.*/
numeric digits 1000                              /*get ka-razy with the decimal digits. */
parse arg num deg .                              /*get optional arguments from the C.L. */
if num=='' | num==","   then num=15              /*Not specified?  Then use the default.*/
if deg=='' | deg==","   then deg=10              /* "      "         "   "   "     "    */
say '═══showing multiple factorials (1 ──►'     deg")  for numbers  1 ──►"      num
say
     do d=1  for deg                             /*the factorializing (degree)  of  !'s.*/
     _=                                          /*the list of factorials  (so far).    */
            do f=1  for num                      /* ◄── perform a ! from  1 ───► number.*/
            _=_  Kfact(f, d)                     /*build a  list  of factorial products.*/
            end   /*f*/                          /* [↑]    D   can default to  unity.   */
 
     say right('n'copies("!", d), 1+deg)    right('['d"]", 2+length(num) )':'     _
     end          /*d*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Kfact: procedure; !=1;   do j=arg(1)  to 2  by -word(arg(2) 1,1);  !=!*j;  end;   return !