Paring function - Output becomes exponential for big real inputs

176 Views Asked by At

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.

1

There are 1 best solutions below

1
On

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.