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)
int
s as input and want a singleint
as output, you will get collisions. There's no way around that. Again what do you need this for? (Yes, I see that your secret use of this function is in a Java program, but you have not told what that use is). – hmakholm left over Monica Nov 18 '18 at 22:58