# How does this code to generate the $k^{th}$ order multifactorial for $n \in \mathbb{N}$ work?

The $$k^{th}$$ order multifactorial for $$n \in \mathbb{N}$$ is defined commonly as the follows:

Which is equivalent to

I saw a succinct line of code on RosettaCode for generating the $$k^{th}$$ order multifactorial for $$n \in \mathbb{N}$$ as follows:

Multifactorial[n_, k_] := Abs[ Apply[ Times, Range[-n, -1, k]]]


Could someone explain me how the code works in a sort of step by step manner since I am unable to quite understand it.

You can start by understanding how Apply works. For example

Apply[f, {a, b, c}]


f[a,b,c]

So, if you need product of these three numbers, you use

Apply[Times, {a, b, c}]


a b c

Now what remains is to find the right elements to multiply. For kth multifactorial of n it is {n,n-k,n-2k,...} which you can write as

Range[n,1,-k]


Your code construct the series in reverse order and so it requires Abs in front. Therefore

Multifactorial[n_, k_] := Apply[Times, Range[n, 1, -k]]


(n)(n-k)(n-2k)...

• Thanks that's a good explanation. Feb 27 '21 at 13:30