ℹ️ 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.4 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://quickmathintuitions.org/why-hash-tables-should-use-prime-number-size/ | ||||||
| Last Crawled | 2026-04-14 09:11:41 (11 days ago) | ||||||
| First Indexed | 2019-04-19 17:06:43 (7 years ago) | ||||||
| HTTP Status Code | 200 | ||||||
| Content | |||||||
| Meta Title | Why hash tables should use a prime-number size — Quick Math Intuitions | ||||||
| Meta Description | Why should hash tables have a prime number for its size? The best size for a hash table has to do with the fact that computers handle data in binary form. | ||||||
| Meta Canonical | null | ||||||
| Boilerpipe Text | I read in several books and online pages that hash tables should use a prime number for the size. Nobody really justified this statement properly. Here’s my attempt!
I believe that it just has to do with the fact that computers work with in base 2. Just think at how the same thing works for base 10:
8 % 10 = 8
18 % 10 = 8
87865378 % 10 = 8
2387762348 % 10 = 8
It doesn’t matter what the number is: as long as it ends with 8, its modulo 10 will be 8. You could pick a huge power of 10 as modulo operator, such as 10^k (with k > 10, let’s say), but
you would need a huge table to store the values
the hash function is still pretty stupid: it just trims the number retaining only the first
k
digits starting from the right.
However, if you pick a different number as modulo operator, such as 12, then things are different:
8 % 12 = 8
18 % 12 = 6
87865378 % 12 = 10
2387762348 % 12 = 8
We still have a collision, but the pattern becomes more complicated, and the collision is just due to the fact that 12 is still a small number.
Picking a big enough, non-power-of-two number will make sure the hash function really is a function of all the input bits, rather than a subset of them.
For example, with 367:
8 % 367 = 8
18 % 367 = 18
87865378 % 367 = 73
2387762348 % 367 = 240
What is worth nothing is that there may be a pattern even with modulo 367, but it would be way less trivial than with modulo 10 (or with modulo 2 in binary).
We don’t really need a prime number
, just having a big non-power of two is enough. Having a prime number, obviously, is just a guaranteed way of satisfying those conditions.
Was this Helpful ?
yes
 Â
no
Post navigation | ||||||
| Markdown | [Skip to content](https://quickmathintuitions.org/why-hash-tables-should-use-prime-number-size/#primary)
[Quick Math Intuitions](https://quickmathintuitions.org/)
Sharing quick intuitions for math ideas
[Computer science](https://quickmathintuitions.org/category/computer-science/)
# Why hash tables should use a prime-number size
[Stefano Ottolenghi](https://quickmathintuitions.org/author/qmi/)
[2017-06-012018-03-12](https://quickmathintuitions.org/why-hash-tables-should-use-prime-number-size/)
[0](https://quickmathintuitions.org/why-hash-tables-should-use-prime-number-size/#respond)
I read in several books and online pages that hash tables should use a prime number for the size. Nobody really justified this statement properly. Here’s my attempt\!
***
I believe that it just has to do with the fact that computers work with in base 2. Just think at how the same thing works for base 10:
- 8 % 10 = 8
- 18 % 10 = 8
- 87865378 % 10 = 8
- 2387762348 % 10 = 8
It doesn’t matter what the number is: as long as it ends with 8, its modulo 10 will be 8. You could pick a huge power of 10 as modulo operator, such as 10^k (with k \> 10, let’s say), but
1. you would need a huge table to store the values
2. the hash function is still pretty stupid: it just trims the number retaining only the first *k* digits starting from the right.
However, if you pick a different number as modulo operator, such as 12, then things are different:
- 8 % 12 = 8
- 18 % 12 = 6
- 87865378 % 12 = 10
- 2387762348 % 12 = 8
We still have a collision, but the pattern becomes more complicated, and the collision is just due to the fact that 12 is still a small number.
**Picking a big enough, non-power-of-two number will make sure the hash function really is a function of all the input bits, rather than a subset of them.**
For example, with 367:
- 8 % 367 = 8
- 18 % 367 = 18
- 87865378 % 367 = 73
- 2387762348 % 367 = 240
What is worth nothing is that there may be a pattern even with modulo 367, but it would be way less trivial than with modulo 10 (or with modulo 2 in binary). **We don’t really need a prime number**, just having a big non-power of two is enough. Having a prime number, obviously, is just a guaranteed way of satisfying those conditions.
- Was this Helpful ?
- [yes](https://quickmathintuitions.org/why-hash-tables-should-use-prime-number-size/) [no](https://quickmathintuitions.org/why-hash-tables-should-use-prime-number-size/)
[hash tables](https://quickmathintuitions.org/tag/hash-tables/)
## Post navigation
[Metaphysics on geometric distribution in probability theory](https://quickmathintuitions.org/metaphysics-on-geometric-distribution-probability-theory/)
[On the meaning of hypothesis and p-value in statistical hypothesis testing](https://quickmathintuitions.org/meaning-hypothesis-p-value-statistical-hypothesis-testing/)
### Leave a Reply [Cancel reply](https://quickmathintuitions.org/why-hash-tables-should-use-prime-number-size/#respond)

#### Categories
- [Algebra](https://quickmathintuitions.org/category/algebra/)
- [Analysis](https://quickmathintuitions.org/category/analysis/)
- [Computer science](https://quickmathintuitions.org/category/computer-science/)
- [Cryptography](https://quickmathintuitions.org/category/cryptography/)
- [Finite Element Methods](https://quickmathintuitions.org/category/finite-element-methods/)
- [Graph theory](https://quickmathintuitions.org/category/graph-theory/)
- [Linear algebra](https://quickmathintuitions.org/category/linear-algebra/)
- [Meta](https://quickmathintuitions.org/category/meta/)
- [Modeling](https://quickmathintuitions.org/category/modeling/)
- [Numerical Analysis](https://quickmathintuitions.org/category/numerical-analysis/)
- [Probability](https://quickmathintuitions.org/category/probability/)
- [Statistics](https://quickmathintuitions.org/category/statistics/)
- [Uncategorized](https://quickmathintuitions.org/category/uncategorized/)
#### [](https://www.thecrowned.org/feeds/all-en.atom.xml) [Blog entries](https://thecrowned.org/)
- [On Karpathy's "Software is changing (again)"](https://thecrowned.org/karpathy-software-is-changing)
- [How many hours does it take to be pissed at France?](https://thecrowned.org/france-joke)
- ["I'm really concerned about Hawai indigenous people's rights"](https://thecrowned.org/philosophy-of-science-class)
- [Don't hurt pomegranates](https://thecrowned.org/pomegranate)
- [What love tells me](https://thecrowned.org/mahler-love)
- [The digital supermarket of human solitude (dating apps)](https://thecrowned.org/dating-apps)
- [How Feldenkrais made me the best math teacher](https://thecrowned.org/feldenkrais-math)
- [A cybersecurity take on the Swedish postal system](https://thecrowned.org/postnord-drama)
- [The dread of teaching pointless math ruins generations](https://thecrowned.org/pointless-math)
- [The differences between junior and senior software engineers](https://thecrowned.org/junior-vs-senior-engineers)
Copyright © 2026 [Quick Math Intuitions](https://quickmathintuitions.org/ "Quick Math Intuitions").
[Proudly powered by WordPress](https://wordpress.org/) \| Theme: Apace by [ThemezHut](https://themezhut.com/themes/apace/). | ||||||
| Readable Markdown | I read in several books and online pages that hash tables should use a prime number for the size. Nobody really justified this statement properly. Here’s my attempt\!
***
I believe that it just has to do with the fact that computers work with in base 2. Just think at how the same thing works for base 10:
- 8 % 10 = 8
- 18 % 10 = 8
- 87865378 % 10 = 8
- 2387762348 % 10 = 8
It doesn’t matter what the number is: as long as it ends with 8, its modulo 10 will be 8. You could pick a huge power of 10 as modulo operator, such as 10^k (with k \> 10, let’s say), but
1. you would need a huge table to store the values
2. the hash function is still pretty stupid: it just trims the number retaining only the first *k* digits starting from the right.
However, if you pick a different number as modulo operator, such as 12, then things are different:
- 8 % 12 = 8
- 18 % 12 = 6
- 87865378 % 12 = 10
- 2387762348 % 12 = 8
We still have a collision, but the pattern becomes more complicated, and the collision is just due to the fact that 12 is still a small number.
**Picking a big enough, non-power-of-two number will make sure the hash function really is a function of all the input bits, rather than a subset of them.**
For example, with 367:
- 8 % 367 = 8
- 18 % 367 = 18
- 87865378 % 367 = 73
- 2387762348 % 367 = 240
What is worth nothing is that there may be a pattern even with modulo 367, but it would be way less trivial than with modulo 10 (or with modulo 2 in binary). **We don’t really need a prime number**, just having a big non-power of two is enough. Having a prime number, obviously, is just a guaranteed way of satisfying those conditions.
- Was this Helpful ?
- [yes](https://quickmathintuitions.org/why-hash-tables-should-use-prime-number-size/) [no](https://quickmathintuitions.org/why-hash-tables-should-use-prime-number-size/)
## Post navigation | ||||||
| ML Classification | |||||||
| ML Categories |
Raw JSON{
"/Computers_and_Electronics": 961,
"/Computers_and_Electronics/Programming": 792
} | ||||||
| ML Page Types |
Raw JSON{
"/Article": 999,
"/Article/Tutorial_or_Guide": 814
} | ||||||
| ML Intent Types |
Raw JSON{
"Informational": 999
} | ||||||
| Content Metadata | |||||||
| Language | en-us | ||||||
| Author | Stefano Ottolenghi | ||||||
| Publish Time | 2017-06-01 08:57:36 (8 years ago) | ||||||
| Original Publish Time | 2017-06-01 08:57:36 (8 years ago) | ||||||
| Republished | No | ||||||
| Word Count (Total) | 549 | ||||||
| Word Count (Content) | 336 | ||||||
| Links | |||||||
| External Links | 14 | ||||||
| Internal Links | 23 | ||||||
| Technical SEO | |||||||
| Meta Nofollow | No | ||||||
| Meta Noarchive | No | ||||||
| JS Rendered | No | ||||||
| Redirect Target | null | ||||||
| Performance | |||||||
| Download Time (ms) | 1,564 | ||||||
| TTFB (ms) | 1,564 | ||||||
| Download Size (bytes) | 12,137 | ||||||
| Shard | 43 (laksa) | ||||||
| Root Hash | 18237700010392947643 | ||||||
| Unparsed URL | org,quickmathintuitions!/why-hash-tables-should-use-prime-number-size/ s443 | ||||||