Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@antimatter15
Created November 30, 2016 05:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antimatter15/8cb2538f4bd195e0b439560ec8c8e5b9 to your computer and use it in GitHub Desktop.
Save antimatter15/8cb2538f4bd195e0b439560ec8c8e5b9 to your computer and use it in GitHub Desktop.
Szudzik's Elegant Pairing Function
// Szudzik's Elegant Pairing Function
// http://szudzik.com/ElegantPairing.pdf
function pair(x,y){
return y > x ? (y*y+x) : (x*x+x+y);
}
function unpair(z){
var q = Math.floor(Math.sqrt(z)),
l = z - Math.pow(q, 2);
return l < q ? [l, q] : [q, l-q];
}
@JeffreySarnoff
Copy link

// Szudzik's Elegant Pairing Function
// http://szudzik.com/ElegantPairing.pdf

function pair(x,y){
return y > x ? (yy+x) : (xx+x+y);
}

function unpair(z){
var q = Math.floor(Math.sqrt(z)),
l = z - Math.pow(q, 2);
return l < q ? [l, q] : [q, l-q];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment