I have 2 different numbers, from example 7 and 8. I need generate third "unique" number using given numbers, that is, if from 7 and 8 we obtain some X number, this X number not will be obtain from some other 2 numbers, X must be generate only from 7 and 8. can make this? Thanks
Generate unique number, from 2 different numbers
-
0Google [pairing function.](http://en.wikipedia.org/wiki/Pairing_function) This is surely a duplicate question. – 2012-09-25
4 Answers
Are the numbers positive integers, or can they be arbitrary real numbers? Does the order of the numbers matter, or can the numbers $7$ and $8$ generate the same number $X$ as the numbers $8$ and $7$?
I will assume that the numbers are positive integers, and that the order of them does not matter. Then you can put all your pairs of numbers in a long sequence, and count along the sequence to get your number $X$. For instance, you can use the sequence $(1,1)\qquad (1,2)\qquad (2,2) \qquad (1,3) \qquad (2,3) \qquad (3,3) \qquad (1,4)\ \ \ \ \ \ \ \ \ \ \ \ $ $(2,4)\qquad (3,4)\qquad (4,4) \qquad (1,5) \qquad (2,5) \qquad (3,5) \qquad (4,5) \quad {\rm etc.}$ Following this pattern, your pair of numbers $7$ and $8$ will then be the $35$th element of this sequence, so $X=35$.
(It is in principle possible to do this for real numbers too, since ${\Bbb R}^2$ and $\Bbb R$ have the same cardinality, but you will not get an explicit expression for $X$ in that case.)
-
0I thought the pattern would be clear - it's the alphabetical ordering of words of length 2. – 2012-09-25
So what you want is an injective function $\mathbb N\times\mathbb N\to\mathbb N$.
There are many possible choices for that -- one of the simplest (among those that don't waste space by leaving many possible results unused) is $(a,b)\mapsto \frac{(a+b)(a+b+1)}2 + a$ which is known as Cantor's zig-zag.
If $a,b$ are nonnegative integers, then you can let $x = \frac{a-b-1+(a+b+1)^2}2.$ This $x$ will always be a non-negative integer and different inputs produce different values of $x$. In fact, this method is not wasteful: All nonnegative numbers $x$ can be obtained by a suitable (and unique) choice of $a$ and $b$.
If the order matters and your two numbers are X and Y you could try things like:
$2^X3^Y$ or $(10^X-1)10^Y$ or $2^X(2Y-1)$
But the original question doesn't state whether X and Y can be zero or negative etc, and the kind of function you choose depends on what you want to use it for.
-
0X and Y, are always positive numbers (not zero also), as I see, more simplest way in this case is, first variant in your answer, thanks very much, thansk everyone. – 2012-09-25