ā¹ļø Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.2 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://programming.guide/prime-numbers-in-hash-tables.html |
| Last Crawled | 2026-04-10 15:14:59 (6 days ago) |
| First Indexed | 2018-06-14 22:02:08 (7 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Why large prime numbers are used in hash tables | Programming.Guide |
| Meta Description | To achieve a good spread of the keys, hash functions must reach the full address space of the hash table. To avoid collisions, the hash value should not have unnecessary factors. |
| Meta Canonical | null |
| Boilerpipe Text | Hash tables store objects in buckets. Which bucket an object ends up in depends on the hash value for the object. To transform a hash value to a valid bucket index you typically just compute modulo numBuckets where numBuckets represents the number of buckets in the hash table. Hash tables perform best when objects are spread evenly among the buckets. A hash function should therefore strive for this goal. Large hash values A hash function that only returns small values, say between 0 and 10, does not achieve a good spread if numBuckets becomes greater than 10. To maintain a good spread when there's a large number of buckets, hash functions typically scale up the values by multiplying with a large prime number. Why prime numbers? If we scaled up the values by a composite number (non-prime), say 1000, there would be many values for numBuckets where the scale would be "cancelled out" in the mod operation. 1000 % numBuckets = 0 for numBuckets = 2, 4, 5, 8, 10, ⦠In other words, similar objects would get similar bucket indexes. This reduces the spread and makes for a poor hash function. By choosing a large prime number, we avoid this problem, since a prime number is not divisible by numBuckets unless numBuckets happens to be a multiple of that particular prime number. Comments |
| Markdown | ## Hash Tables
1. [Hash Tables](https://programming.guide/hash-tables.html)
2. [Complexity](https://programming.guide/hash-tables-complexity.html)
3. [Hash Table Load Factor and Capacity](https://programming.guide/hash-table-load-factor-and-capacity.html)
4. [Hash Table vs Hash Set](https://programming.guide/hash-table-vs-hash-set.html)
5. [Open vs Closed Addressing](https://programming.guide/hash-tables-open-vs-closed-addressing.html)
6. [Open Addressing](https://programming.guide/hash-tables-open-addressing.html)
7. [Coalesced Hashing](https://programming.guide/coalesced-hashing.html)
8. [Cuckoo Hashing](https://programming.guide/cuckoo-hashing.html)
9. [Robin Hood Hashing](https://programming.guide/robin-hood-hashing.html)
10. [Hopscotch Hashing](https://programming.guide/hopscotch-hashing.html)
11. [2-Choice Hashing](https://programming.guide/2-choice-hashing.html)
12. [2-Left Hashing](https://programming.guide/2-left-hashing.html)
13. [Linked Hash Table](https://programming.guide/linked-hash-table.html)
14. Why large prime numbers are used in hash tables
## Top Algorithm Articles
1. [Dynamic programming vs memoization vs tabulation](https://programming.guide/dynamic-programming-vs-memoization-vs-tabulation.html)
2. [Big O notation explained](https://programming.guide/big-o-notation-explained.html)
3. [Sliding Window Algorithm with Example](https://programming.guide/sliding-window-example.html)
4. [What makes a good loop invariant?](https://programming.guide/what-makes-a-good-loop-invariant.html)
5. [Generating a random point within a circle (uniformly)](https://programming.guide/random-point-within-circle.html)
[**See all algorithm articles**](https://programming.guide/algorithms.html)
# Why large prime numbers are used in hash tables
Hash tables store objects in buckets. Which bucket an object ends up in depends on the hash value for the object.
To transform a hash value to a valid bucket index you typically just compute modulo `numBuckets` where `numBuckets` represents the number of buckets in the hash table.
Hash tables perform best when objects are **spread evenly** among the buckets. A hash function should therefore strive for this goal.
## Large hash values
A hash function that only returns small values, say between 0 and 10, does not achieve a good spread if `numBuckets` becomes greater than 10.
To maintain a good spread when there's a large number of buckets, hash functions typically scale up the values by multiplying with a large prime number.
## Why prime numbers?
If we scaled up the values by a composite number (non-prime), say 1000, there would be many values for `numBuckets` where the scale would be "cancelled out" in the mod operation. 1000 % `numBuckets` = 0 for `numBuckets` = 2, 4, 5, 8, 10, ⦠In other words, similar objects would get similar bucket indexes. This reduces the spread and makes for a poor hash function.
By choosing a large prime number, we avoid this problem, since a prime number is not divisible by `numBuckets` unless `numBuckets` happens to be a multiple of that particular prime number.
## Comments
Be the first to comment\!
Ā© 2016ā2022 Programming.Guide, [Terms and Conditions](https://programming.guide/terms-and-conditions.html) |
| Readable Markdown | null |
| Shard | 154 (laksa) |
| Root Hash | 10469987800461033954 |
| Unparsed URL | guide,programming!/prime-numbers-in-hash-tables.html s443 |