Pairing function for ordered pairs

786 Views Asked by At

Is there a pairing function like Cantor's (https://en.wikipedia.org/wiki/Pairing_function) that would map ordered pairs (of integers) to different integers? ie:

(M, N) -> L1

(N, M) -> L2

Where L1 != L2

All input integers could be positive, but the output does not have to be positive..so perhaps something like:

newpair(M, N) = if (M < N): cantorpair(M, N)
                else: -1 * cantorpair(M, N)

Or is there some well known / standard pairing function for ordered 2-tuples?

1

There are 1 best solutions below

1
On BEST ANSWER

Cantor's pairing function should already work, as does a prime number encoding, e.g. $$ \langle M, N \rangle = 2^M 3^N $$ Example: $$ \langle 1, 2 \rangle = 2^1 3^2 = 18 \\ \langle 2, 1 \rangle = 2^2 3^1 = 12 $$