# Math terminology question… Non redundant ordered pairs in a list.

I’m trying to mathematically produce non redundant, sequential pairs in a list, so if the number is 2 it outputs the list: 1 2 (it would NOT output 1 1 1 2 2 1 2 2). If the incoming number is 3 it outputs: 1 2 1 3 2 3. If 4 output 1 2 1 3 1 4 2 3 2 4 3 4. See the pattern? I need to be able to do this to an incoming number of 52. For prototyping I’ve just been using coll and did the first 8 or so manually.

I just don’t know enough math to understand what this is called, specifically so I can recognize the correct formula. I can build the external (or perhaps there’s one or two objects that do the same), no problem. I’m just stuck on the math.

Thanks, would really appreciate any help.

try this

**all**of the following text. Then, in Max, select

*New From Clipboard*.

```
----------begin_max5_patcher----------
880.3oc0XtsbaBCDF9Z7SgJ2V6LHIN4N8ll65yPmLYvfbhZ.gGinwMYx6dQR
lXRivbLJIiGaLKXyteZ2+cgGWXYuI+.ovF7Mvu.VVOtvxRZRXv539V1YQGhS
iJjmlcbdVFgwsWpNFmbfKs+8Uwoz36psSSjVy276UvvZiayYbVTFQdnermFk
BtLOMo9v6h3w2RY2b8dRLW4SPGuKbVBPghOCBjeu5SvUG+IrxLJKkvk9F7jw
7RdsUmFW7B5CxKNr5+SX8oEKDerrmwdFonH5FxqhcOjtvNX7gsm6on1U8cu1
hZj1nF1RTezr5L4+cGQcAssAWMi.IeGgoAIn1IxYfARB.rKVtQlCHhjISCzb
SCF49pn7UvHNOMEjTFk9SVB4vkka2Bf5xW7GEcB7TXQBIHTUt3LrRD2ghmkM
d+7EpH5OjjqqBkJe65HNeOcSIWItX8LwrrIYaHxnVVApX7LQZNXCfpCsdiCs
J8Fe4FL5BugSVz.I6lH1MBpRqTXmybvcQw2AnhW5vCdb34XcoHbpUrZEO32w
5x1ToJHbfiNdLNcJHBq5O47oUnpjwI60Pj0So9Y8ZYya74SP7lEkIQcSCwoQ
WF01TNaoLZwsjDcYMvwk0DpTWf8oJZ.S3fF0DNsE2xdXTQ+qkfxc6H6q2IM+
95cp+QoTFQlJ0LMu4T.iqQGTUQgwR8FnihatuanpkhnupK0vYJ0OJAVry46+
LO5IyceGYG4pWa.wojHcRKgiBMdx4b78UC8DLlpFuIzZ9zlZqpvaNQW4C55U
GLAbgfRkXW34wk9LI7Hw0a0DM7VGmweBEaPOIh7fiIiBOtlUM1Lm.5Kqzvlo
LHLLTVt4E7YTHZk164xcB3HbcetsfOlzPu1BbBZKPryGHsE4EWNHx+8rsj9u
v9KYVQd4935+85GdC3TLjPJ3TVDmlyZbRgu3btkljPXMGxHilrKux2N5Csr.
1WWRb0b6viDOCgg3RPeY5K1OTrIz84clnuJtixNw2Pc1YfevNbIWi6QcBIi6
QnN7HuWvQS3Q3tXDxnPR7zj5bcas4cotRtkEklakSbSOcmd6YTN42i7aIlPF
ykV2GJgMa1D5CWBdPeV3bLpK0GUf.iVw41y9IFTCnGKa9FWA.1mDInQcImdz
z0wnKa8I01rdTWKalcJ.za8DtU67zh+AfRYOF.
-----------end_max5_patcher-----------
```

edit: btw if there's an easier way to do this I'd like to know…

Think you are on the right track, still wish I knew what this was called and how to express it mathmatically.

Thanks for the suggestion! It’s quite the contraption! :)

I am not sure there is a mathematical name for that kind of sequence, but I guess you could describe it as a subsequence of the natural numbers interleaved with its lowest element or something like that.

Here is a js version:

```
seq = function (start, n) {
if (start >= n)
return [];
var i, res = [];
for (i = start + 1; i < = n; i++) {
res.push(start);
res.push(i);
}
return res.concat(seq(start + 1, n));
}
```

hey, those are great… interesting stuff!

You don’t say what you want to do with the result, but that might influence how you manage the result. For example, the number 52 results in 1,326 possible unique pairings (i.e. 2,652 numbers), which makes it impractical to output the result in Max as a single list (as you described in your question). You’d probably want to store the result in a Jitter matrix (or a coll, as Terry did).

Yep, it becomes pretty big fast. That’s the wonder of exponential growth ;).

the number of pairs will be n*(n-1)/2, given a maximum n and combining from 1 to n

I think they’re called combinations as (distinct from permutations)

and that’s as far as my maths knowledge allows me to go

and as Hans has eloquently illustrated, much easier to do in a text-based language rather than in max

Hmmm… Might have to start rethinking my approach. I knew the order was close to 1000 pairs, but you are right that would be impractical.

You know, I really need to read up on this more. I’m doing 2D collision detection between objects, with 52 possible objects on screen at any point. I was using a "good enough" approach for up to 8 possible collisions but trying to go higher is just proving a headache.

Perhaps I need to find a more efficient approach to this and code it in an external.