Given two integers and
, where
is non-negative, an ordered
-partition of
is a
-tuple
of positive integers such that
For example, the ordered 3-partitions of 5 are ,
,
,
,
, and
.
The problem I'm considering in this post is: how do you enumerate all the ordered -partititions of
?
In the case where , the problem is simple. An ordered 0-partition is a 0-tuple. But there is only one 0-tuple: the empty tuple,
. And the sum of
is 0, which means the only integer
is an ordered 0-partition of is 0. So 0 has
as its only ordered 0-partition, while every other integer has no ordered 0-partitions.
In other cases, where , we can try to reduce the problem to instances of the problem where
is smaller, so that we can use recursion to arrive at a solution. Observe that an ordered
-partition of
will have the form
, where
, …,
are positive integers that sum to
. Given that
, we can talk about
individually. By subtracting
from both sides of the equation
, we obtain the equivalent equation
. This proves the following theorem:
Theorem. The ordered -partitions of
are the
-tuples
of positive integers such that
is an ordered
-partition of
.
This gives us a nice mathematical recursive characterization of the ordered -partitions. If we write the set of all ordered
-partitions of
as
, and we denote concatenation of tuples by
, we can write the following equations:
However, these equations do not translate directly into a terminating algorithm, because of the union over all of the infinitely many positive integers taken in the third equation.
But we don't necessarily need to examine all positive integers , because for some of them,
may be empty, so that nothing is contributed to the overall union by this particular value of
. This suggests that we should consider the following sub-problem:
For which integers
and
, where
is non-negative, are there no ordered
-partitions of
?
This is easy to solve:
Theorem. For every integer and every nonnegative integer
, the following statements are equivalent:
has an ordered
-partition.
or
.
Proof.
- If
is negative, then it has no ordered
-partitions, because ordered
-partitions of
are tuples of positive integers that sum to
, but the sum of a tuple of positive integers is always non-negative.
- If
, then
has no ordered
-partitions, because ordered
-partitions of
are tuples of
positive integers that sum to
, and the sum of
posiive integers cannot be less than
.
- 0 has an ordered 0-partition, namely
. But no nonzero integer
has any ordered 0-partitions.
- If
, then the ordered
-tuple consisting of
1s, followed by
(which is positive since
), is an ordered
-partition of
.
So, in the third equation above, the union only needs to be taken over values of such that
(i.e.
and
) or
(i.e.
and
). Hence we can rewrite the equations as follows:
This can now be translated straightforwardly to code in a programming language. For example, here's a Python implementation:
from typing import Iterator
def int_parts(n: int, k: int) -> Iterator[tuple[int, ...]]:
if k == 0:
if n == 0:
yield ()
return
if k == 1:
if n >= 1:
yield (n,)
return
for m in range(1, n - k + 2):
for p in int_parts(n - m, k - 1):
yield (m,) + p
This is a pretty trivial subject, and I feel like my presentation of it here is somewhat incomplete (for example, I haven't said anything about how efficient the algorithm described here is). However, I think my standards for considering a post "complete" are a bit too high at the moment, as evidenced by the fact that I haven't made any posts on this blog for a couple of years, despite having written a number of unfinished drafts.
I think it would be good if whenever I learn something new, I would write down what I've learnt that very same day and make a blog post out of it, regardless of how trivial or in need of further exploration it might seem. So I'm going to attempt to do that from now on.