Created
March 29, 2017 01:06
-
-
Save torjusti/e068872c1d31c0441417677fe37b330f to your computer and use it in GitHub Desktop.
Generalized Cantor pairing function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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