Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

There is a series of good answers on StackOverflowStackOverflow and Quora, where I learned these go by the name pairing functions (related to dovetailing in computer science), can be generalized to any infinite sets, and iterated to encode arbitrary-length tuples as @Hugo illustrates.

In particular Szudzik's function is relatively efficient in computation and representation:

$$ ElegantPair (x, y) = \begin{cases} y^2 + x & x \neq max(x, y)\\ x^2 + x + y & x = max(x, y)\\ \end{cases} $$

and its inverse:

$$ ElegantUnpair (z) = \begin{cases} (z - \lfloor{\sqrt{z}}\rfloor^2, \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 < \lfloor{\sqrt{z}}\rfloor\\ (\lfloor{\sqrt{z}}\rfloor, z - \lfloor{\sqrt{z}}\rfloor^2 - \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 \geq \lfloor{\sqrt{z}}\rfloor\\ \end{cases} $$

Which looks like this:

enter image description here

There is a series of good answers on StackOverflow and Quora, where I learned these go by the name pairing functions (related to dovetailing in computer science), can be generalized to any infinite sets, and iterated to encode arbitrary-length tuples as @Hugo illustrates.

In particular Szudzik's function is relatively efficient in computation and representation:

$$ ElegantPair (x, y) = \begin{cases} y^2 + x & x \neq max(x, y)\\ x^2 + x + y & x = max(x, y)\\ \end{cases} $$

and its inverse:

$$ ElegantUnpair (z) = \begin{cases} (z - \lfloor{\sqrt{z}}\rfloor^2, \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 < \lfloor{\sqrt{z}}\rfloor\\ (\lfloor{\sqrt{z}}\rfloor, z - \lfloor{\sqrt{z}}\rfloor^2 - \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 \geq \lfloor{\sqrt{z}}\rfloor\\ \end{cases} $$

Which looks like this:

enter image description here

There is a series of good answers on StackOverflow and Quora, where I learned these go by the name pairing functions (related to dovetailing in computer science), can be generalized to any infinite sets, and iterated to encode arbitrary-length tuples as @Hugo illustrates.

In particular Szudzik's function is relatively efficient in computation and representation:

$$ ElegantPair (x, y) = \begin{cases} y^2 + x & x \neq max(x, y)\\ x^2 + x + y & x = max(x, y)\\ \end{cases} $$

and its inverse:

$$ ElegantUnpair (z) = \begin{cases} (z - \lfloor{\sqrt{z}}\rfloor^2, \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 < \lfloor{\sqrt{z}}\rfloor\\ (\lfloor{\sqrt{z}}\rfloor, z - \lfloor{\sqrt{z}}\rfloor^2 - \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 \geq \lfloor{\sqrt{z}}\rfloor\\ \end{cases} $$

Which looks like this:

enter image description here

Add image of function
Source Link

There is a series of good answers on StackOverflow and Quora, where I learned these go by the name pairing functions (related to dovetailing in computer science), can be generalized to any infinite sets, and iterated to encode arbitrary-length tuples as @Hugo illustrates.

In particular Szudzik's function is relatively efficient in computation and representation:

$$ ElegantPair (x, y) = \begin{cases} y^2 + x & x \neq max(x, y)\\ x^2 + x + y & x = max(x, y)\\ \end{cases} $$

and its inverse:

$$ ElegantUnpair (z) = \begin{cases} (z - \lfloor{\sqrt{z}}\rfloor^2, \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 < \lfloor{\sqrt{z}}\rfloor\\ (\lfloor{\sqrt{z}}\rfloor, z - \lfloor{\sqrt{z}}\rfloor^2 - \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 \geq \lfloor{\sqrt{z}}\rfloor\\ \end{cases} $$

Which looks like this:

enter image description here

There is a series of good answers on StackOverflow and Quora, where I learned these go by the name pairing functions (related to dovetailing in computer science), can be generalized to any infinite sets, and iterated to encode arbitrary-length tuples as @Hugo illustrates.

In particular Szudzik's function is relatively efficient in computation and representation:

$$ ElegantPair (x, y) = \begin{cases} y^2 + x & x \neq max(x, y)\\ x^2 + x + y & x = max(x, y)\\ \end{cases} $$

and its inverse:

$$ ElegantUnpair (z) = \begin{cases} (z - \lfloor{\sqrt{z}}\rfloor^2, \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 < \lfloor{\sqrt{z}}\rfloor\\ (\lfloor{\sqrt{z}}\rfloor, z - \lfloor{\sqrt{z}}\rfloor^2 - \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 \geq \lfloor{\sqrt{z}}\rfloor\\ \end{cases} $$

There is a series of good answers on StackOverflow and Quora, where I learned these go by the name pairing functions (related to dovetailing in computer science), can be generalized to any infinite sets, and iterated to encode arbitrary-length tuples as @Hugo illustrates.

In particular Szudzik's function is relatively efficient in computation and representation:

$$ ElegantPair (x, y) = \begin{cases} y^2 + x & x \neq max(x, y)\\ x^2 + x + y & x = max(x, y)\\ \end{cases} $$

and its inverse:

$$ ElegantUnpair (z) = \begin{cases} (z - \lfloor{\sqrt{z}}\rfloor^2, \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 < \lfloor{\sqrt{z}}\rfloor\\ (\lfloor{\sqrt{z}}\rfloor, z - \lfloor{\sqrt{z}}\rfloor^2 - \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 \geq \lfloor{\sqrt{z}}\rfloor\\ \end{cases} $$

Which looks like this:

enter image description here

Source Link

There is a series of good answers on StackOverflow and Quora, where I learned these go by the name pairing functions (related to dovetailing in computer science), can be generalized to any infinite sets, and iterated to encode arbitrary-length tuples as @Hugo illustrates.

In particular Szudzik's function is relatively efficient in computation and representation:

$$ ElegantPair (x, y) = \begin{cases} y^2 + x & x \neq max(x, y)\\ x^2 + x + y & x = max(x, y)\\ \end{cases} $$

and its inverse:

$$ ElegantUnpair (z) = \begin{cases} (z - \lfloor{\sqrt{z}}\rfloor^2, \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 < \lfloor{\sqrt{z}}\rfloor\\ (\lfloor{\sqrt{z}}\rfloor, z - \lfloor{\sqrt{z}}\rfloor^2 - \lfloor{\sqrt{z}}\rfloor) & z - \lfloor{\sqrt{z}}\rfloor^2 \geq \lfloor{\sqrt{z}}\rfloor\\ \end{cases} $$