Imagine two positive integers A and B. I want to combine these two into a single integer C.
There can be no other integers D and E which combine to C.
So combining them with the addition operator doesn't work. Eg 30 + 10 = 40 = 40 + 0 = 39 + 1
Neither does concatination work. Eg "31" + "2" = 312 = "3" + "12"
This combination operation should also be deterministic (always yield the same result with the same inputs) and should always yield an integer on either the positive or the negative side of integers.
Best Answer
You're looking for a bijective
NxN -> N
mapping. These are used for e.g. dovetailing. Have a look at this PDF for an introduction to so-called pairing functions. Wikipedia introduces a specific pairing function, namely the Cantor pairing function:Three remarks:
ZxZ -> N
mapping. Cantor's function only works on non-negative numbers. This is not a problem however, because it's easy to define a bijectionf : Z -> N
, like so: