Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash Function for 3 Integers

Tags:

hashcode

hash

I have 3 non-negative integers and a number n such that

0 <= a <= n, 0 <= b <= n, and 0 <= c <= n. 

I need a one-way hash function that maps these 3 integers to one integer (could be any integer, positive or negative). Is there a way to do this, and if so, how? Is there a way so that this this function can be expressed as a simple mathematical expression where the only parameters are a, b, c, and n?

Note: I need this function because I was using tuples of 3 integers as keys in a dictionary on python, and with upwards of 10^10 keys, space is a real issue.

like image 246
Andi Gu Avatar asked Oct 29 '25 15:10

Andi Gu


1 Answers

How about the Cantor pairing function (https://en.wikipedia.org/wiki/Pairing_function#Cantor_pairing_function)?

Let H(a,b) := .5*(a + b)*(a + b + 1) + b

then

H(a,b,c) := .5*(H(a,b) + c)*(H(a,b) + c + 1) + c

You mentioned that you need a one-way hash, but based on your detailed description about memory constraints it seems that an invertible hash would also suffice.

This doesn't use the assumption that a, b, and c are bounded above and below.

like image 65
tzarhaxaar Avatar answered Oct 31 '25 11:10

tzarhaxaar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!