A smilar question has been asked before Create unique number from 2 numbers.
is there some way to create unique number from 2 positive integer numbers? Result must be unique even for these pairs: 2 and 30, 1 and 15, 4 and 60. In general, if I take 2 random numbers result must be unique(or with very high probability unique)Mine should be Unique
Thanks a lot
EDIT: calculation is for computer program,so computational complexity is important
The aceepted answer suggest the user to use Cantor pairing function which I tried to program it like this:
To calculate π(47, 32):
47 + 32 = 79,
79 + 1 = 80,
79 × 80 = 6320,
6320 ÷ 2 = 3160,
3160 + 32 = 3192,
so π(47, 32) = 3192.
To find x and y such that π(x, y) = 1432:
8 × 1432 = 11456,
11456 + 1 = 11457,
√11457 = 107.037,
107.037 − 1 = 106.037,
106.037 ÷ 2 = 53.019,
⌊53.019⌋ = 53,
so w = 53;
53 + 1 = 54,
53 × 54 = 2862,
2862 ÷ 2 = 1431,
so t = 1431;
1432 − 1431 = 1,
so y = 1;
53 − 1 = 52,
so x = 52; thus π(52, 1) = 1432.
π(a,b)=12(a+b)(a+b+1)+b.
But the Canton Pairing doesn't always create a unique number based on the two numbers used e.g:
a=73114279
b=1
π(a,b)=663902325
But when you inverse it, using the inverse fomular such that:
π(x,y)=663902325
You would get
π(-536868496,536884434)=663902325
My question is: What formular can I use so that I can always get a unique number from just two numbers, though the formular must be inversible such that:
(a,b)=c (b,a)=d
But when you inverse it, using the inverse fomular such that:
You would get c=(a,b) d=(b,a)
If order is important--i.e., the number you create from $(a,b)$ must be different from $(b,a)$--then exploit the Unique Factorization Theorem to create the number:
$n = {\rm Prime}[a]^2 \rm{Prime}[b]$ where ${\rm Prime}[a]$ is the $a^{th}$ prime number.
In Mathematica:
For two numbers above $10^9$, this takes $0.030089$ seconds on a Mac laptop. Not wasteful at all... extremely efficient... And of course provably unique.
Of course, to go backward, you merely perform a Factorization:
(* {{3, 2}, {113, 1}} *)
which means $1017 = 3^2 \times 113$
This tells you that the first element, $a$, is such that the $a^{th}$ prime is $3$, and the second element, $b$, is such that the $b^{th}$ prime is $113$.
You can easily and efficiently find these numbers:
(* 2 *)
(* 30 *)
In simple, efficient code:
So for the example you requested:
(* {3,7} *)