0
$\begingroup$

I am using a Cantor pairing function that takes two real number output unique real number.

def cantor_paring(a,b):
    return (1/2)*(a+b)*(a+b+1) + b

This work good for me when the input pair number are small.

cantor_paring(3,5)
41.0

However, when the input pair number is big the output becomes very huge.

cantor_paring(195149767,9877)
1.9043643420693068e+16

Now question I have is, is there a way to tweak the pair function so that output is relatively small even for big input numbers.

$\endgroup$
1
  • 1
    $\begingroup$ No. $\phantom{\text{Think yourself why.}}$ $\endgroup$
    – metamorphy
    Commented Aug 23, 2020 at 1:45

1 Answer 1

1
$\begingroup$

The one-word comment by metamorphy correctly answers the question, but I guess you might want a little more detail. If you consider integers in the range from $1$ to $n$, you can form $n^2$ ordered pairs of them. So to code them as positive integers, you'll need to use integers at least up to $n^2$. Those integers will have about twice as many digits as the integers you began with. And that's what you're seeing: $k$-digit integers need approximately $2k$-digit codes.

$\endgroup$
1
  • $\begingroup$ In the question, you referred to Cantor's pairing function as mapping pairs of real numbers to real numbers. It does, but it doesn't then serve as a pairing function; many input pairs produce the same output. It's a pairing function for natural numbers. (This also makes your use of floating-point arithmetic look weird.) $\endgroup$ Commented Aug 23, 2020 at 2:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .