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 Feb 24 '11 at 9:17
  • 1
    $\begingroup$ Are the pairs ordered? Should 2 and 30 produce the same answer as 30 and 2? $\endgroup$ – Henry Feb 24 '11 at 10:02
  • $\begingroup$ 2and30,30and2 are two different pairs $\endgroup$ – Cicik Feb 24 '11 at 10:52
  • 6
    $\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 Mar 8 '11 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 Mar 9 '11 at 1:41

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$ – Cicik Mar 8 '11 at 21:24
  • $\begingroup$ If you're looking for something of minimal computational complexity for a computer to handle, you could use an "infinite hotel" type construction where you think of the number as its binary expansion -- a list of 0's and 1's and then you "merge" the list by taking one term from each number, in succession. $\endgroup$ – Ryan Budney Mar 8 '11 at 22:21
  • $\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$ – Rhymoid Dec 24 '12 at 18:19
  • 2
    $\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$ – Placeholder Oct 21 '14 at 12:53
  • $\begingroup$ @GANGSTAIRL seems like a plus to me :)! $\endgroup$ – Thomas W Apr 17 '17 at 12:26

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.

  • 5
    $\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 Sep 2 '16 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:

  1. Here's an unordered pairing function:

    $<x, y> = x * y + trunc(\frac{(|x - y| - 1)^2}{4}) = <y, x>$

  2. 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 Mar 10 '14 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.

  • $\begingroup$ Nice answer. I've rewritten the function in notation which will be more familiar to mathematicians. $\endgroup$ – Alex Kruckman Feb 4 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 Feb 21 '18 at 9:29
  • 1
    $\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 Mar 20 '18 at 5:19

protected by user99914 Mar 20 '18 at 4:54

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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