Skip to content

Instantly share code, notes, and snippets.

@torjusti
Created March 29, 2017 01:06
Show Gist options
  • Save torjusti/e068872c1d31c0441417677fe37b330f to your computer and use it in GitHub Desktop.
Save torjusti/e068872c1d31c0441417677fe37b330f to your computer and use it in GitHub Desktop.
Generalized Cantor pairing function
const cantor = (arr) => {
if (arr.length === 2) {
return 1 / 2 * (arr[0] + arr[1]) * (arr[0] + arr[1] + 1) + arr[1];
}
return cantor(
arr.slice(0, arr.length - 2)
.concat(
cantor([arr[arr.length - 1], arr[arr.length - 2]])
)
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment