# Elegant Pairing Function (Szudzik)

An Elegant Pairing Function implementation for JavaScript

When `x`

and `y`

are non-negative integers, `elegantPair(x, y)`

function outputs single non-negative integer that is uniquely associated with that pair.
The inverse function `elegantUnpair(z)`

outputs the pair assscociated with each non-negative integer `z`

.

` ````
//An Elegant Pairing Function by Matthew Szudzik @ Wolfram Research, Inc.
function elegantPair(x, y) {
return (x >= y) ? (x * x + x + y) : (y * y + x);
}
function elegantUnpair(z) {
var sqrtz = Math.floor(Math.sqrt(z)),
sqz = sqrtz * sqrtz;
return ((z - sqz) >= sqrtz) ? [sqrtz, z - sqz - sqrtz] : [z - sqz, sqrtz];
}
```

Here's an other one, but bitwise

Thanks for bitwise pairing function :) but it will work only for 16 bit numbers (up to 65536).

@sachmata Indeed, however for 2d digital positioning, this ought to be enough. It's a "favor speed over size" thing :p