Why is the size 127 (prime) better than 128 for a hash-table?

后端 未结 9 1386
深忆病人
深忆病人 2021-01-30 08:22

Supposing simple uniform hashing, that being, any given value is equally like to hash into any of the slots of the hash. Why is it better to use a table of size 127 and not 128?

9条回答
  •  耶瑟儿~
    2021-01-30 09:04

    Wikipedia actually has a good summary of this:

    http://en.wikipedia.org/wiki/Hash_table

    They point out that some hash functions are designed to operate ONLY with prime numbers. This article explains why powers of two are bad:

    http://www.concentric.net/~Ttwang/tech/primehash.htm

提交回复
热议问题