Sorceress

Sorcerean pairing function

Dec 12th, 2020
1,714
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Sorcerean pairing function.
  3.  *
  4.  * @see @_sorceress
  5.  */
  6. export const pair = (k1: number, k2: number): number => (
  7.     ((k1 > k2) ? k1 * k1 + k2 : k2 * (k2 + 2) - k1) | 0
  8. );
  9.  
  10.  
  11. /**
  12.  * Inverse to the Sorcerean pairing function.
  13.  *
  14.  * @see @_sorceress
  15.  */
  16. export const depair = (z: number): [number, number] => {
  17.     const w = Math.floor(z);
  18.     const t = z - w * w;
  19.     if(t > w) {
  20.         return [2 * w - t, w];
  21.     } else {
  22.         return [w , t];
  23.     }
  24. }
RAW Paste Data