Why do we want to use prime number (prime number) for modulo operation--hash table design

Source: Internet
Author: User
Tags hash
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.