Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why a good choice of mod is "a prime not too close to an exact of 2"

To generate a hash function, Map a key k into one of m slots by taking the remainder of k divided by m. That is, the hash function is

h(k) = k mod m.

I have read at several places that a good choice of m will be

  1. A prime - I understand that we want to remove common factors, hence a prime number is chosen
  2. not too close to an exact power of 2 - why is that?
like image 424
learner Avatar asked Dec 04 '14 07:12

learner


People also ask

Why is it best to use a prime number as a mod in a hashing function?

Edit: As a summary, primes are used because you have the best chance of obtaining a unique value when multiplying values by the prime number chosen and adding them all up. For example given a string, multiplying each letter value with the prime number and then adding those all up will give you its hash value.

Why are prime numbers important in hashing?

For hashing, prime numbers are used since they provide a better chance of creating unique values for a hash function.


1 Answers

From Introduction to algorithms :

When using the division method we avoid certain values of m. For example m should not be power of 2. Since if m=2^p then h(k) is p lowest-order bits of k. Unless it is known that all low-order p-bit patterns are equally likely,
it is better to make a hash function depend on all bits of the key.

As you se from the below image if i chose 2^3 which mean p=3 and m=8. The hashed keys are only dependent to lowest 3(p) bits which is bad because when you hash you want to include as much data as possible for a good distribution.

enter image description here

like image 64
Salih Erikci Avatar answered Oct 09 '22 10:10

Salih Erikci