I'm having a programming problem, but I feel this would be more relevant here. I have three unique variables, $x$, $y$, and $z$. What operations can I perform on these variables to produce a unique value? Are there any approaches I could take?


EDIT: To be more specific, I want some function $f(x, y, z)$ that produces a unique output for each set of inputs (assuming uniqueness in inputs, $1$, $2$, $3$ does not need to output something different than $3$, $2$, $1$). I realize that a generalized case of this is probably not possible, but I would definitely be able to co-opt similar solutions

  • $\begingroup$ I'm not quite sure what you're asking. x+ y + z would produce a unique value I feel you're after something else $\endgroup$
    – mrnovice
    Feb 24, 2017 at 4:32
  • $\begingroup$ Consider the case (1, 4, 3) and (2, 2, 4). They add to the same value, what I want is a single or set of operations I can perform on these three numbers to get a unique value. Of course, I only need these operations to work within a reasonable size limit, I am writing a computer program after all $\endgroup$
    – bren
    Feb 24, 2017 at 4:35
  • $\begingroup$ So given $x, y, z$, you want an operation which outputs a value $a \neq x,y,z$? $\endgroup$
    – mrnovice
    Feb 24, 2017 at 4:38
  • $\begingroup$ @mrnovice My edit should provide more information. Thanks! $\endgroup$
    – bren
    Feb 24, 2017 at 4:39
  • 1
    $\begingroup$ You could consider normalising each (making them of same length by appending zeroes) and computing an MD5sum or likewise, using some other algorithm. This is not completely foolproof as per your condition (search for md5 common attack), but it should work for all practical purposes. Again this is not exactly what you're looking for but I just felt this could go in a comment. $\endgroup$
    – axolotl
    Feb 24, 2017 at 5:29

5 Answers 5


$f(x,y,z) = x\sqrt{2} + y\sqrt{5} + z\sqrt{7}$

This function would increase by a similar order of magnitude to its inputs. Moreover it's possible to prove that for $x,y,z \in \mathbb{Z}, f(x,y,z)$ must be unique $\forall x,y,z$.

Edit: Proof:

Suppose for contradiction that $\exists x_{1},y_{1},z_{1},x_{2},y_{2},z_{2} \in \mathbb{Z}$:

$f(x_{1},y_{1},z_{1}) = f(x_{2},y_{2},z_{2})$ with at least one of the following being true: $x_{1} \neq x_{2}, y_{1} \neq y_{2} , z_{1} \neq z_{2}$

Then we have $x_{1}\sqrt{2} + y_{1}\sqrt{5} + z_{1}\sqrt{7} = x_{2}\sqrt{2} + y_{2}\sqrt{5} + z_{2}\sqrt{7}$

$\Rightarrow (x_{1} - x_{2})\sqrt{2} + (y_{1} - y_{2})\sqrt{5} + (z_{1} - z_{2})\sqrt{7} = 0$

In the case that $x_{1} = x_{2}$ and $y_{1} \neq y_{2}$ and $z_{1} \neq z_{2}$:

$\frac{\sqrt{5}}{\sqrt{7}} = \frac{z_{2} - z_{1}}{y_{1}-y_{2}}$

This would imply that $\frac{\sqrt{5}}{\sqrt{7}} \in \mathbb{Q}$ which leads to a contradiction (distinct irrational divided by distinct irrational must also be irrational - simple to prove)

A similar proof applies to the case where $y_{1} = y_{2}, x_{1} \neq x_{2}$ and $z_{1} \neq z_{2}$ and the case when $z_{1} = z_{2}, x_{1} \neq x_{2}$ and $y_{1} \neq y_{2}$

If we have $x_{1} = x_{2}$ and $y_{1} = y_{2}$, then we get $z_{1} = z_{2}$ which is a contradiction. Therefore we have proved the following conditions:

$x_{1} \neq x_{2}, y_{1} \neq y_{2} , z_{1} \neq z_{2}$

let $a = x_{1} - x_{2}, b = -(y_{1}-y_{2}), c = -(z_{1} - z_{2})$

Then we have $a\sqrt{2} = b\sqrt{5} + c\sqrt{7}$

$\Rightarrow \sqrt{2} = \frac{b\sqrt{5} + c\sqrt{7}}{a}$

let $b_{1} = \frac{b}{a}, c_{1} = \frac{c}{a}$

$\Rightarrow b_{1}, c_{1} \in \mathbb{R}$

$2 = (b_{1}\sqrt{5} + c_{1}\sqrt{7})^{2}$

$\sqrt{35} = 2 - 5b_{1}^{2} - 7c_{1}^{2}$

$\Rightarrow \sqrt{35} \in \mathbb{Q}$ which is a contradiction.

This implies that $\forall x,y,z \in \mathbb{Z}, f(x,y,z)$ is unique.

  • $\begingroup$ could you offer some insight into how one could go proving this? $\endgroup$
    – axolotl
    Feb 24, 2017 at 5:26
  • $\begingroup$ I added the proof, sorry it took a while $\endgroup$
    – mrnovice
    Feb 24, 2017 at 6:11
  • $\begingroup$ That's all well and good but I can prove that your function isn't one-to-one. Counter example: Let $u= (\sqrt 5,0,a)$ and $v=(0,\sqrt 2,a)$. Then $f(u)=\sqrt 5*\sqrt2+0+a\sqrt7=0+\sqrt 5*\sqrt2+a\sqrt7 = f(v)$. $\endgroup$ Feb 24, 2017 at 6:37
  • $\begingroup$ I mentioned at the start that $x,y,z \in \mathbb{Z}$ $\endgroup$
    – mrnovice
    Feb 24, 2017 at 6:40
  • 1
    $\begingroup$ Okay, then it would work if $f:\mathbb Z^3\to \mathbb R$.I just thought that we were trying to find an $f: \mathbb R^3 \to \mathbb R$. $\endgroup$ Feb 24, 2017 at 6:42

Thye can be coerced to become integers

If the inputs can be further coerced to be non-negative integers in known ranges $0 \le x \lt a\,$, $0 \le y \lt b\,$, $0 \le z \lt c\,$, then the function $f(x,y,z) = x + a \,y + ab \,z\,$ can be easily shown to be injective, and its inverse is readily calculatable $x = f \;\%\; a\,$, $y = (f/a) \;\%\; b\,$, $z=f/(ab)\,$ where "$/$" denotes integer division (with truncation towards $0$) and "$\%$" is the modulo operator .

  • $\begingroup$ If you take $a = 2, b = 4$, then $f(0,2,0) = f(0,0,1)$ and therefore the output is not unique, unless I've missed something $\endgroup$
    – mrnovice
    Feb 24, 2017 at 5:28
  • $\begingroup$ @mrnovice You must have miscalculated $f(0,2,0)=2 \cdot 2 = 4 \ne 8 = 2 \cdot 4 \cdot 1 = f(0,0,1)\,$. $\endgroup$
    – dxiv
    Feb 24, 2017 at 5:35

If you're working with non-negative integers with "nice" bounds to keep the numbers from being too large (or allowing unbounded using arbitrary-size integer typing), then one way to handle it, that keeps the overall sizes of the numbers relatively bounded (so three small numbers still produces a (relatively) small number) is to riffle the binary representations. That is, if you wanted 2,5,9, then you would write them in binary (with the same length):

0010, 0101, 1001

Then you would take digits successively... $$ \begin{pmatrix}0 & 0 & 1 & 0\\0 & 1 & 0 & 1\\1 & 0 & 0 & 1\end{pmatrix}^T = \begin{pmatrix}0 & 0 & 1\\0 & 1 & 0\\1 & 0 & 0\\0 & 1 & 1\end{pmatrix} $$ I'm representing it as above for ease of visualisation. Now write as a single number:

$001010100011 = 675$

The nice thing about this is that all triples map uniquely onto an integer, and all integers are mapped to - that is, given any integer, there is a triple that maps to it. Also, increasing any value in the triple will increase the resulting number (although it won't be a linear increase).

The downside is that it'll take some coding to make it work in most languages (I do know that it would be moderately easy in Julia, where you could convert to string as binary using bin with some padding, and then merge the strings with a bit of easy code, and then parse back to integer), and it probably won't be fast code (although you might be able to implement it using some loops with basic arithmetic/bitwise operations).

Note that it need not be binary - you can do the same operations in decimal, so $(13, 51, 9) \to 150319$


mmovice is close with his sample solution. The only issue that will pop up is when the values are 0. A good way to do this is which is similar. Let $f: \mathbb R^3 \to \mathbb R$ such that the $(x,y,z)$ values are alternated with respect to their decimal value. as an example $(10,214,3)= 020,110,043$ this eliminates the issues with zero values.

  • $\begingroup$ Could you elaborate? $\endgroup$
    – bren
    Feb 24, 2017 at 5:16
  • $\begingroup$ So lets try to explain how it's made. were going to separate the decimal places of each value. into the sets for $X$, $Y$, $Z$ such that $x_i=\frac{x- x mod 10^{i-1}}{10^i}mod 10^i \in X$. we do the same thing with $y_j \in Y$ and $z_k\in Z$. So $f(x,y,z)= \sum_i \sum_j \sum_k x_i*10^{3i+2} +y_j*10^{3j+1} +z_k 10^{3k}$. Keep in mind this works with integers and can be expanded to the reals but it's more tricky. $\endgroup$ Feb 24, 2017 at 5:44

You can use the concept of binary expression.for example, (1,2,3) goes to the binary expression of 123.


You must log in to answer this question.

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