Why do we want to use prime numbers (prime numbers) for modulo operations?

Source: Internet
Author: User

A good hash function needs to distribute the original data evenly into the hash array.

The original data is not really random, there may be some regularity,

For example, most are even, this time if the hash array capacity is even, it is easy to make the original data hash will not be evenly distributed.
For example 2 4 6 8 10 12 This 6 number, if the 6 to get the remainder of 2 4 0 2 4 0 will only get 3 hash values, the conflict will be many
If you get 2 4 6 1 3 5 for 7 gain, you get 6 hash values, no conflict

Similarly, if the data is a multiple of 3, and the hash array capacity is a multiple of 3, hash is also prone to conflict
Using a prime number reduces the probability of conflict

Reference

Http://www.xuebuyuan.com/2177964.html

http://www.vvbin.com/?p=376

Why do we want to use prime numbers (prime numbers) for modulo operations?

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.