Enumerating all p-tuples of positive integer lower or equal than k that sum up to n.

applied.math.coding
2 min readFeb 2, 2022

In this small account we consider the following combinatorial problem:

Enumerate all p-tuples of non-negative integers lower or equal than k that sums up to n.

Example: p = 2, n = 2, k = 2:

(1, 1), (0, 2), (2, 0)

Example: p = 3, n = 2, k = 2:

(1, 1, 0), (0, 0, 2), (2, 0, 0), (0, 1, 1), (1, 0, 1), (0, 2, 0)

For problems like this, it is always more desirable to have an iterator than just an algorithm that produces all results in a huge list. The reason lies at hand, the former is using a minimal amount of memory while the latter loads up everything into memory.

Rust:

--

--

applied.math.coding

I am a Software Developer - Java, Rust, SQL, TypeScript - with strong interest doing research in pure and applied Mathematics.