Article 5QQK4 Multifactorial

Multifactorial

by
John
from John D. Cook on (#5QQK4)

The factorial of an integer n is the product of the positive integers up to n.

The double factorial of an even (odd) integer n is the product of the positive even (odd) integers up to n. For example,

8!! = 8 * 6 * 4 * 2

and

9!! = 9 * 7 * 5 * 3 * 1.

Double factorials come up fairly often, and sometimes triple, quadruple, or higher multifactorials do too.

In general, the k-factorial of n is the product of the positive integers up to n that are congruent to n mod k. Here's how you might implement k-factorials in Python.

 from operator import mul from functools import reduce def multifactorial(n, k): return reduce(mul, [i for i in range(1, n+1) if (n-i)%k == 0], 1)

Update: As pointed out in the comments, multifactorial could be implemented more sucinctly as

 def multifactorial(n, k): return reduce(mul, range(n, 0, -k), 1)

Factorial notation is a little awkward, and multifactorial notation is even more awkward. Someone seeing n!! for the first time might reasonably assume it means (n!)!, but it does not.

Adding exclamation points works, albeit awkwardly, for specific values of k, but not for variable numbers of k. One way I've seen to express k-factorials for variable k is to add a subscript (k) to the factorial:

multifactorial1.svg

I've also seen the subscript on the exclamation point written as a superscript instead.

I'd like to suggest a different notation: k. by analogy with the function.

pisubk.svg

When k 1 the condition i n (mod k) always holds, and we have factorial, i.e.

1(n) = (n) = n!.

One downside of this notation is that while the function is defined for all complex numbers (aside from singularities at negative integers) the function k is only defined for positive integer arguments. Still, in my opinion, this is less awkward than the alternatives I've seen.

By the way, the term double factorial" seems backward. Maybe it should have been half factorial" because you're multiplying half as many numbers together, not twice as many. Multifactorial" in general seems like an unfortunate name. Subfactorial might have been better, but unfortunately that name means something else.

I understand why someone may have come up with the idea of using two exclamation points for what we call double factorial; it would be hard to draw half an exclamation point. An advantage of the notation suggested here is that it suggests that there's a variation on factorial somehow associated with k, but there's no implication of multiplying or dividing by k.

Adding mod k" as a subscript would be even more explicit than a subscript k. Someone who hadn't seen

mod k (n)

before might be able to figure out what it means; I think they would at least easily remember what it means once told. And the presence of mod" might suggest to the reader that the argument needs to be an integer.

The post Multifactorial first appeared on John D. Cook.rirEyKL9aYI
External Content
Source RSS or Atom Feed
Feed Location http://feeds.feedburner.com/TheEndeavour?format=xml
Feed Title John D. Cook
Feed Link https://www.johndcook.com/blog
Reply 0 comments