dinex
dinex

Reputation: 377

Mapping two integers into one integer in Python 3

I would like to map two integers into one for hash function in class. Those integers range from 0 to 12856320000000. I have heard that I can use the below Cantour pairing function to do so

enter image description here

However, my sys.maxsize is

9223372036854775807

As a result, when k1 and k2 are large as the following example, the answer will not be integer

k1 = 6887309306
k2 = 6887309307
pi(k1,k2)=9.487005898057764e+19

Is there anyway to fix this problem? Thanks

Upvotes: 1

Views: 308

Answers (1)

DYZ
DYZ

Reputation: 57033

Your problem is caused not by the number, but by the fact that you divide by 2. Division in Python 3.x always produces a floating point number. You should use integer division:

(k1 + k2) * (k1 + k2 + 1) // 2 + k2
#94870058980577640498

Upvotes: 3

Related Questions