Given an algorithm that uses an modulo hash function, meaning big numbers bigger than a certain given integer will "wrap around" so the result is always in between 0 and the given integer. For example the Rabin-Karp Algorithm needs a rolling hash, with a clever modulo. What is the highest modulo possible? And why is that?
Asked
Active
Viewed 57 times
-1
-
There is no highest modulo possible. In general, the bigger the modulo the better the hash, but the slower it is to compute. – Paul Hankin Mar 08 '17 at 23:56
-
Why wouldn't there be a limit? Have you ever considered a 32-bit integer, with modulo 2^(31)-1? This will not work, because any sufficient large enough integer multiplication, will cause the integer to overflow, before you can take the modulo. – Rich_Rich Mar 10 '17 at 14:54
1 Answers
-1
To answer this question it is important to check what the "highest-impact" operation is between to modulo calls. This means that if you expect a number optimally to be multiplied with 97*101=9797, you have to devide 2^(32) by 9797 (from the Rabin-Karp example), and then look for the nearest prime in this area. If you for instance add 2600 to an integer, you have to substract 2600 from 2^(32). If you square a given number, you have to take the squareroot from 2^(32), etc.
This will then prevent that any overflow will happen after an operation and before the modulo call. Therefore always keeping the hashed number in between 0 and the selected prime.
Because you have selected a (high) prime, the number of hash collisions is as low as possible.

Rich_Rich
- 427
- 3
- 15