2

Is it possible to produce unique number (an integer) from two integers?

The two numbers are points in 2d coordinate system, their value can be 0, negative and positive.

I can assume that all points will be in range +- (-1000,1000)

my first guess was simply use a1*n+a2*m (where n and m are randomly choosen primes) however this formula produces some conflicts.

qwertik
  • 21

2 Answers2

2

Yes, there are a number of different ways. If you have a defined range, it's easiest to normalize the numbers to start at $0$: $a_1' = a_1 + 1000$, $a_2' = a_2 + 1000$. From there, you can use the Cantor pairing function: $\pi(a_1', a_2') = \frac{1}{2}(a_1' + a_2')(a_1' + a_2' + 1) + a_2'$. You can also use the uniqueness of prime factorization: $2^{a_1'} \cdot 3^{a_2'}$ will give you a unique natural number.

DylanSp
  • 1,727
  • 1
  • 12
  • 14
1

For a defined range it is easy. For your $[-1000,+1000]$ in each axis, just take $(a,b) \to 2001(a+1000)+(b+1000)$ The $+1000$ gets rid of the negatives and you are writing it in base $2001$ This is as efficient as you can get in terms of the maximum number produced.

Ross Millikan
  • 374,822