Why do I need a prime number (prime number) for modulo operations--hash table design by admin | July 25, 2013 | Essays, Programming techniques
When designing a hash table with division to scatter, we will use the value modulo hash table size to get the remainder to be stored as ID in the hash table corresponding lattice. All articles indicate that a larger prime number is used as the size of the hash table, which is to model a larger prime number. But why do you use prime numbers? Simple analysis can see the mystery.
Let's see if you use a composite 8 as the hash table size, 0-30 scatter in the Hashtable:
(table 1)
Let's take a look at the scattering situation in the hash table with prime number 7 as the Hashtable size, 0-30:
(Table 2)
As we all know, composite 8 has a factor of 2 and 4 in addition to 1 and itself. Observation in a single column of table 1 can be found, these in the same column number, they are actually the last number +8, and view 2, 4, 6 of these three rows we found that because 2 4 6 can be divisible by 2 (or 4), and the number on the same column after +8 satisfied can be divisible by 2 (or 4) this feature. For example, 4 of this column, 4, 12, 20, 28, these hash map in the same lattice is the number of the previous number +8, and then they can be divided by 2 and 4, which leads to a strong relationship between them, it is easy to hash conflict.
Take a look at table 2, in the same case, the number in the same column is obtained from the previous number +7, but because 7 is a prime, it has no other factor than the 1 and itself, so in the same column of the number we can not find the characteristics we just said.
And we all know that the hash table is designed to want random scattering as much as possible, do not want these elements on the same column (that is, conflicting elements) have a relationship, so we all take the prime number as the size of the hash table, so as to avoid a common factor between the same modulus.