TAOCP 7.2.1.4 Exercise 65
Let $m=\prod_{p} p^{E_p}$ be the prime factorization of $m$, where each $E_p\ge 0$.
Section 7.2.1.4: Generating All Partitions
Exercise 65. [23] [23] It is well known that every commutative group of m elements can be repre- sented as a discrete torus T(m1, . . . , mn) with the addition operation of 7.2.1.3–(66), where m = m1 . . . mn and mj is a multiple of mj+1 for 1 ≤j < n. For example, when m = 360 = 23 · 32 · 51 there are six such groups, corresponding to the factorizations (m1, m2, m3) = (30, 6, 2), (60, 6, 1), (90, 2, 2), (120, 3, 1), (180, 2, 1), and (360, 1, 1). Explain how to generate all such factorizations systematically with an algorithm that changes exactly two of the factors mj at each step.
Verified: no
Solve time: 3m55s
Solution
Let $m=\prod_{p} p^{E_p}$ be the prime factorization of $m$, where each $E_p\ge 0$. A valid factorization
$$ (m_1,\ldots,m_n) \quad\text{with}\quad m=\prod_{j=1}^n m_j,\qquad m_j \mid m_{j-1}\ (2\le j\le n) $$
is equivalent to assigning to each prime $p$ an exponent sequence
$$ e_{p,1}\ge e_{p,2}\ge \cdots \ge e_{p,n}\ge 0, \qquad \sum_{j=1}^n e_{p,j}=E_p, $$
and then defining
$$ m_j=\prod_p p^{e_{p,j}}. $$
The divisibility condition $m_j \mid m_{j-1}$ is equivalent to $e_{p,j}\le e_{p,j-1}$ for every prime $p$, hence each prime contributes independently as a partition of $E_p$ into at most $n$ nonincreasing parts.
Thus the set of all factorizations is the Cartesian product, over primes $p\mid m$, of the sets
$$ \mathcal{P}(E_p,n)={(e_{p,1},\ldots,e_{p,n}) : e_{p,1}\ge\cdots\ge e_{p,n}\ge 0,\ \sum_j e_{p,j}=E_p}. $$
For each fixed prime $p$, Algorithm P of Section 7.2.1.4 generates all such partitions of $E_p$ into $n$ parts by successive operations that modify a suffix of the sequence. Each transition in Algorithm P changes exactly two entries of the exponent sequence: one entry is decreased by $1$ at some position $q$, and one entry is increased by $1$ at a position determined by the redistribution step in P6 when a unit is shifted forward as $1+1$. Every replacement in that algorithm corresponds to one unit transfer between two coordinates, hence to exactly two modified exponent positions.
Consequently, if only one prime’s exponent sequence is changed between successive global states, then exactly two of the numbers $m_j$ change, since only two coordinates $e_{p,i}$ and $e_{p,j}$ change and
$$ m_i \mapsto m_i\cdot p^{\Delta}, \qquad m_j \mapsto m_j\cdot p^{-\Delta}, $$
with all other $m_k$ unchanged.
It remains to generate the Cartesian product of the sets $\mathcal{P}(E_p,n)$ in such a way that successive tuples differ in exactly one component. Order the primes as $p_1,\ldots,p_r$.
For each $p_i$, fix the reverse lexicographic ordering of $\mathcal{P}(E_{p_i},n)$ generated by Algorithm P, and denote its successive elements by $P_i^{(t)}$.
Construct a reflected product traversal as follows. Maintain indices $t_i$ for each prime component and a direction bit $\sigma_i\in{+1,-1}$. Start with $t_i=1$ for all $i$ and all $\sigma_i=+1$. The global state is
$$ (m_1,\ldots,m_n), \quad m_j=\prod_{i=1}^r p_i^{,e^{(t_i)}_{p_i,j}}. $$
The traversal proceeds in a mixed-radix reflected Gray order: increment $t_1$ while possible; when $t_1$ reaches an endpoint, reverse direction $\sigma_1$ and advance $t_2$ by one step; when $t_2$ reaches an endpoint, reverse $\sigma_2$ and advance $t_3$, and so on. This is the standard reflected product construction ensuring that between successive global states exactly one index $t_i$ changes.
At each transition, only one prime component $p_i$ changes its exponent sequence from one partition $P_i^{(t_i)}$ to the next. By the structure of Algorithm P, that change modifies exactly two coordinates of $(e_{p_i,1},\ldots,e_{p_i,n})$, hence exactly two values among $(m_1,\ldots,m_n)$ are multiplied or divided by $p_i$, while all other components remain fixed.
Thus every step of the global algorithm changes exactly two of the factors $m_j$.
To verify completeness, every admissible factorization corresponds uniquely to a tuple of partitions $(P_1^{(t_1)},\ldots,P_r^{(t_r)})$, and the reflected product traversal visits every such tuple exactly once by induction on $r$, since each coordinate list is exhausted in full forward and reverse order before the next coordinate changes.
The construction therefore enumerates all factorizations of $m$ with the required divisibility condition, and consecutive outputs differ in exactly two factors $m_j$.
This completes the proof. ∎