Integer partition into sums and products
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Partitioning an integer into sums and partitioning an integer into products are related ideas, but they are not the same problem. Additive partitions ask how a number can be written as a sum of positive integers when order does not matter, while multiplicative partitions ask how it can be written as a product of integers greater than 1, again ignoring order.
Additive Partitions
For additive partitions, order is irrelevant. The number 5 has these partitions:
- '
5' - '
4 + 1' - '
3 + 2' - '
3 + 1 + 1' - '
2 + 2 + 1' - '
2 + 1 + 1 + 1' - '
1 + 1 + 1 + 1 + 1'
A recursive way to generate them is to keep the next part no larger than the previous one. That guarantees combinations are produced once instead of in many reordered forms.
The max_part argument enforces non-increasing order, which is the key to avoiding duplicates.
Multiplicative Partitions
Multiplicative partitions work similarly in spirit, but the recursion uses divisors rather than candidate addends. For 12, the multiplicative partitions are:
- '
12' - '
6 × 2' - '
4 × 3' - '
3 × 2 × 2'
Again, order does not matter, so 2 × 6 is the same as 6 × 2.
The min_factor parameter plays the same role as max_part in the additive case: it prevents duplicates caused by reordering.
Why These Problems Feel Similar
Both problems are recursive decompositions of an integer under an order-insensitive rule. In both cases, the important trick is to impose an ordering constraint so that each partition is generated once.
But their mathematical behavior is different.
For additive partitions, every positive integer has many possible decompositions, and the number of partitions grows rapidly. For multiplicative partitions, the prime factorization of the number strongly constrains the possibilities. Prime numbers, for example, have only the trivial multiplicative partition consisting of the number itself.
Dynamic Programming for Counting Additive Partitions
If you only need the number of additive partitions, not the full list, dynamic programming is usually better than generating every partition.
This works because each part size is introduced in increasing order, preventing permutation duplicates automatically.
When Sums and Products Meet in the Same Question
Sometimes the phrase “partition into sums and products” means a hybrid puzzle, such as finding expressions where grouped sums are multiplied, or comparing additive and multiplicative decompositions of the same integer. In those cases, the exact problem statement matters a lot.
For example, these are very different tasks:
- list all additive partitions of
n - list all multiplicative partitions of
n - find a partition of
nwhose product is maximal - split
ninto terms that satisfy both sum and product constraints
The algorithms differ once the question becomes optimization instead of enumeration.
Complexity and Practical Limits
Enumerating all partitions grows expensive quickly. Even when the recursive generator is elegant, the output size itself becomes large. That means no algorithm can list all partitions quickly for large n because the answer contains too many objects.
In practice:
- use recursion when you need the actual partitions for small or moderate
n - use dynamic programming when you only need counts
- use divisor-based recursion for multiplicative partitions
Choosing the wrong approach usually means generating far more structure than the program really needs.
Common Pitfalls
A common mistake is forgetting that order does not matter. Without an ordering constraint, recursive code will generate duplicates such as [3, 2] and [2, 3].
Another mistake is allowing 1 as a multiplicative factor repeatedly. That creates endless trivial decompositions, so multiplicative partitions usually restrict factors to values greater than 1.
Developers also confuse counting partitions with listing them. Counting can often be done with dynamic programming, while listing necessarily scales with the size of the output.
Finally, make sure the problem statement is specific about whether the trivial partition containing only n should be included.
Summary
- Additive partitions decompose an integer into sums, ignoring order.
- Multiplicative partitions decompose an integer into factors greater than
1, also ignoring order. - Recursive generation works well when you enforce an ordering rule to avoid duplicates.
- Dynamic programming is often better when you only need counts for additive partitions.
- Hybrid “sum and product” questions need a precise statement because the algorithm depends on the exact constraint.

