I'm looking for a way of generating all permutations of three digits (actually xyz) that sum to the same value. For example: n = 1: 0 0 0 n = 2: 0 0 1 0 1 0 1 0 0 n = 3: 0 1 1 1 0 1 1 1 0 0 0 2 0 2 0 2 0 0 n = 4: 1 1 2 ... Ideally I need a way of mapping indices to the three digits in a unique way such that I can take an index up to the associated triangle number n * (n + 1) / 2 and find the same three indices every time. Can anyone point me in the right direction? It's quite some time since I've worked with sequences and I'm a little rusty.