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, 2011 at 9:17
  • 3
    $\begingroup$ Are the pairs ordered? Should 2 and 30 produce the same answer as 30 and 2? $\endgroup$
    – Henry
    Feb 24, 2011 at 10:02
  • $\begingroup$ 2and30,30and2 are two different pairs $\endgroup$
    – drizzt
    Feb 24, 2011 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, 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
    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
    Mar 8, 2011 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$ Mar 8, 2011 at 22:21
  • 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$
    – Rhymoid
    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$ Oct 21, 2014 at 12:53
  • 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$ 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
    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:

  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$ 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$ 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$ 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$ Mar 20, 2018 at 5:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .