3
$\begingroup$

I have a pair of positive integers $(x, y)$, with $(x, y)$ being different from $(y, x)$, and I'd like to calculate an integer "key" representing them in order that for a unique $(x, y)$ there is an unique key and no other pair $(w, z)$ could generate the same key.

I don't mind if we can/can't guess the pair values based on the key.

The key must be < 2,147,483,647 and the value of $x$ and $y$ as high as possible.

Any solution ?

$\endgroup$
  • $\begingroup$ Are these positive or non-negative integers?? $\endgroup$ – robjohn Oct 19 '13 at 8:50
  • $\begingroup$ Sorry i forgot to tell it (question is edited now), these are positive integer (>= 0) $\endgroup$ – IggY Oct 19 '13 at 8:57
  • 1
    $\begingroup$ @IggY It's better to avoid the term 'positive integer', tecause in the Czech Republic, it means $x\geq1$ while in France it means $x\geq0$ ;) So the think you have in mind are 'non-negative integers' (as opposed to 'strictly positive integers'). $\endgroup$ – yo' Oct 19 '13 at 9:04
  • $\begingroup$ @tohecz: I had no idea that in someplaces "positive integers" included $0$. I will have to be more careful. $\endgroup$ – robjohn Oct 19 '13 at 9:47
  • $\begingroup$ If the arguments are to be less than $2^{15}$ then the key can be chosen so that it is less than $2^{30}$. If the arguments range from $0$ to $2^{16}-1$ then the key can only be unique if it can reach $2^{32}-1$. The condition regarding $2^{31}-1$ is perplexing. $\endgroup$ – robjohn Oct 19 '13 at 10:01
5
$\begingroup$

You could use something like $\text{key}(x,y) = 2^{15}x + y$, where $x$ and $y$ are both restricted to the range $[0, 2^{15}]$. In programming terms, this just means stuffing $x$ into (roughly) half of your 31-bit space and $y$ into the other half.

It sounds like you are using a 32-bit integer variable in some programming language to represent the key. This is the reason for the limit of 2,147,483,647, I suppose. This means you really only have 31 bits available, because one bit is reserved for the sign of the integer. But this is slightly wasteful. If you use an unsigned integer variable (which is available in most programming languages), then you'll have 32 bits available. You can then use 16 bits for $x$, and 16 bits for $y$.

Using this sort of approach, recovering $x$ and $y$ from a given key is easy to code and extremely fast -- you just grab the relevant bits from the key variable. I can write the code, if you don't know how.

If you still want to use an int variable for the key (k), then the code (in C#) is:

int k = 32768*x + y;

With this approach, we require $x \le 65535$ (16 bits) and $y \le 32767$ (15 bits), and k will be at most 2,147,483,647, so it can be held in a 32-bit int variable.

If you are willing to use an unsigned int variable for the key (k), instead, as I suggested, then the code (again in C#) is:

uint k = 65536*x + y;

With this approach, we require $x \le 65535$ (16 bits) and $y \le 65535$ (16 bits), and k will be at most 4,294,967,295, which will fit in a 32-bit unsigned int variable.

$\endgroup$
  • $\begingroup$ Thank you for the edit, my english is pretty bad :s AS i said in my question, the key must be < 2,147,483,647, as it cannot be an negative number (a number > 2,147,483,647 would be considered negative). So it makes your answer being 2^8*x+y : is this the better solution ? $\endgroup$ – IggY Oct 19 '13 at 8:53
  • $\begingroup$ Well $2147483647 = 2^{31} - 1$, so we have 31 bits to use, I guess. So, each of $x$ and $y$ can occupy 15 bits. So maybe I should have said $k(x,y) = 2^{15}x + y$. Anyway, you get the idea -- stuff two 2-byte unsigned integers into a 4-byte word. $\endgroup$ – bubba Oct 19 '13 at 8:59
6
$\begingroup$

I recommend the Cantor Pairing Function (wiki) defined by

$$\pi(x,y)=\frac12(x+y)(x+y+1)+y$$

The advantage is that when $x,y<K$ you have $\pi(x,y)<2(K+1)^2$, so you don't get extremly large keys with small values of $x$ and $y$.

The inverse function is described at the wiki page.


If you want to have all paris $x,y<2^{15}$, then you can go with the Szudzik's function:

$$\sigma(x,y)=\begin{cases}x^2+x+y & \text{if }x\geq y\\ x+y^2 & \text{otherwise}\end{cases}$$

$\endgroup$
  • $\begingroup$ That's a nice idea, however after a fast calculation I think (with key < 2,147,483,647) the solution of bubba authorise higher values for x & y $\endgroup$ – IggY Oct 19 '13 at 8:55
  • $\begingroup$ @IggY Sorry, both are bijections, what's your point again? This is not a programming forum, but still, remember that int can have different length depending on the architecture, so it might not be the best idea to rely on its size. Btw, cf. stackoverflow.com/questions/919612/… $\endgroup$ – yo' Oct 19 '13 at 8:59
  • 1
    $\begingroup$ @IggY: I was just about to post this same answer. The question says nothing about the numbers being restricted to a given range. However, the statement about the key being less than $2^{31}-1$ was a bit perplexing. $\endgroup$ – robjohn Oct 19 '13 at 9:00
2
$\begingroup$

Another unique key for non-negative integer pairs is

$$\mathrm{key}(x,y)=\max(x,y)^2+\max(y,2y-x)$$

This covers squares first, so should meet your conditions as well as not being limited to a given argument maximum. That is, if $\max(x,y)\lt2^n$, then $\mathrm{key}(x,y)\lt2^{2n}$.

$\hspace{4cm}$enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.