0
$\begingroup$

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

Which is equivalent to

enter image description here

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.

$\endgroup$
2
$\begingroup$

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)...

$\endgroup$
1
  • $\begingroup$ Thanks that's a good explanation. $\endgroup$ – Bhoris Dhanjal Feb 27 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.