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)
Thanks a lot
EDIT: calculation is for computer program,so computational complexity is important
-
$\begingroup$ You do not specify what you mean by making a number out of two numbers. $\endgroup$shuhalo– shuhalo2011年02月24日 09:17:06 +00:00Commented Feb 24, 2011 at 9:17
-
3$\begingroup$ Are the pairs ordered? Should 2 and 30 produce the same answer as 30 and 2? $\endgroup$Henry– Henry2011年02月24日 10:02:01 +00:00Commented Feb 24, 2011 at 10:02
-
$\begingroup$ 2and30,30and2 are two different pairs $\endgroup$drizzt– drizzt2011年02月24日 10:52:13 +00:00Commented Feb 24, 2011 at 10:52
-
7$\begingroup$ I don't get the bounty. Alex Kruckman gave a very good answer whose computational complexity is essentially $O(1)$ if I am not mistaken. Can you please specify what is wrong with the answer given so far? $\endgroup$Asaf Karagila– Asaf Karagila ♦2011年03月08日 22:20:52 +00:00Commented Mar 8, 2011 at 22:20
-
$\begingroup$ I agree with Asaf. Alex Kruckman's answer mentions a quite fast pairing function. If you are really concerned about the efficiency of the algorithm (for reading and writing), then you should probably look for some other ways of combining two numbers, like a list or an ordered pair, or whatever your language supports. But as far as standard mathematical pairing functions go, Cantor's is reasonably quick. Of course, it would be much easier to answer your question appropriately if we knew what your motivation is. $\endgroup$Abel– Abel2011年03月09日 01:41:35 +00:00Commented Mar 9, 2011 at 1:41
6 Answers 6
The sort of function you are looking for is often called a pairing function.
A standard example is the Cantor pairing function $\mathbb{N}\times\mathbb{N}\rightarrow\mathbb{N},ドル given by: $\pi(a,b) = \frac{1}{2}(a+b)(a+b+1) + b$.
You can find more information here: http://en.wikipedia.org/wiki/pairing_function
-
$\begingroup$ thanks, this is good advice. can you suggest me which pairing function has the smallest computational complexity? $\endgroup$drizzt– drizzt2011年03月08日 21:24:56 +00:00Commented Mar 8, 2011 at 21:24
-
1$\begingroup$ That's not necessarily the cheapest way, it depends on what you're counting. In most actual computers, there is a multiplication at a reasonable price, while zipping two binary expansions will take many operations. The Cantor pairing function is the cheapest in actual applications, with only three(!) additions, one multiplication, and a single right shift (to get the $\tfrac12$). $\endgroup$user44190– user441902012年12月24日 18:19:46 +00:00Commented Dec 24, 2012 at 18:19
-
7$\begingroup$ Just to make it clear, this formula DOES NOT GENERATE the same output when you change the order of A and B, e.g: f(A,B) != f(B,A). $\endgroup$Purple Dragon– Purple Dragon2014年10月21日 12:53:40 +00:00Commented Oct 21, 2014 at 12:53
-
1$\begingroup$ @ChristopherDumas No - one reason your suggestion doesn't work is that $\Pi(a,b,c) = \Pi(c,b,a)$. There are many "tripling" functions $\mathbb{N}\times\mathbb{N}\times \mathbb{N}\to \mathbb{N}$. One way to generalize any pairing function $\pi$ to a tripling function $\Pi$ is to define $\Pi(a,b,c) = \pi(\pi(a,b),c)$. When $\pi$ is the Cantor pairing function from my answer: $$\Pi(a,b,c) = \frac{1}{2}\left(\frac{1}{2}(a+b)(a+b+1)+b+c\right)\left(\frac{1}{2}(a+b)(a+b+1)+b + c + 1\right) + c.$$ $\endgroup$Alex Kruckman– Alex Kruckman2019年02月04日 15:07:44 +00:00Commented Feb 4, 2019 at 15:07
-
3$\begingroup$ @hobwell Yes (0ドル\in \mathbb{N}$). If you visit the wikipedia page I linked to in my answer, you'll find a graphical representation of what this function does, and you can see clearly from that picture that it includes ordered pairs of natural numbers of the form $(0,b)$ and $(a,0)$ in the domain of the function. $\endgroup$Alex Kruckman– Alex Kruckman2019年11月06日 22:07:20 +00:00Commented Nov 6, 2019 at 22:07
if the numbers are $a$ and $b,ドル take 2ドル^a3^b$. This method works for any number of numbers (just take different primes as the bases), and all the numbers are distinct.
-
8$\begingroup$ This method generates exceptionally large numbers fairly quickly. (2^1023 on it's own is 8.9885E+307). If you're using this in excel 2^1023.9 is your upper limit) $\endgroup$Bradley– Bradley2016年09月02日 23:05:34 +00:00Commented Sep 2, 2016 at 23:05
Google pairing function. As I mentioned in the similar question, there are also other pairing functions besides the well-known one due to Cantor. For example, see this "elegant" pairing function, which has the useful property that it orders many expressions by depth.
For positive integers as arguments and where argument order doesn't matter:
Here's an unordered pairing function:
$<x, y> = x * y + trunc(\frac{(|x - y| - 1)^2}{4}) = <y, x>$
For x ≠ y, here's a unique unordered pairing function:
<x, y> = if x < y: x * (y - 1) + trunc((y - x - 2)^2 / 4) if x > y: (x - 1) * y + trunc((x - y - 2)^2 / 4) = <y, x>
-
2$\begingroup$ If you look in the comments, the OP specifically notes that for their application, argument order does matter. $\endgroup$Steven Stadnicki– Steven Stadnicki2014年03月10日 06:59:27 +00:00Commented Mar 10, 2014 at 6:59
You could try the function by Matthew Szudzik, which is given as: $$a \geq b ~?~ a*a + a + b : a+b*b$$
In other words: $$(a,b)\mapsto \begin{cases} a^2 + a + b & \text{if } a \geq b\\ a + b^2 & \text{if } a < b \end{cases}$$
I found it here, which is a similar question as this.
-
1$\begingroup$ Nice answer. I've rewritten the function in notation which will be more familiar to mathematicians. $\endgroup$Alex Kruckman– Alex Kruckman2019年02月04日 15:21:35 +00:00Commented Feb 4, 2019 at 15:21
Apologies for resurrecting this ancient question, but I've noticed that there are collisions in the results of the Cantor pairing function.
For example, I've noticed that when a and b are 0.0 and 0.0, or 0.6 and 0.0 the result is the same: 0.48.
Is this function expected to work for non-discrete numbers, or does this have something to do with the fact they the numbers are 0.0 and 0.0 in the former case?
-
$\begingroup$ Welcome to MSE. Please use MathJax. $\endgroup$José Carlos Santos– José Carlos Santos2018年02月21日 09:29:07 +00:00Commented Feb 21, 2018 at 9:29
-
2$\begingroup$ The Cantor pairing function only takes natural numbers for its input. If you want to use rationals or reals you need to work harder. $\endgroup$Ross Millikan– Ross Millikan2018年03月20日 05:19:39 +00:00Commented Mar 20, 2018 at 5:19