How to go from cantorPairFunction back to original number pair

  javascript

I have a cantorPairFunction to get a unique number from two numbers shown below:

const cantorPair = (x, y) => (0.5 * (x + y) * (x + y + 1)) + y

function cantorPairSigned(x, y) {
  const a = (x >= 0.0 ? 2.0 * x : (-2.0 * x) - 1.0);
  const b = (y >= 0.0 ? 2.0 * y : (-2.0 * y) - 1.0);
  return cantorPair(a, b);
}

An example would be:

console.log(cantorPairSigned(-7, 4)) // 239

Is there any way to go from 239 back to the original two numbers labelled x and y?
For example:

console.log(reverseCantorPairSigned(239)) // {x: -7, y: 4}

Source: Ask Javascript Questions

LEAVE A COMMENT