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

后端 未结 9 1297
深忆病人
深忆病人 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:12

    I believe that it just has to do with the fact that computers work with in base 2. Something similar happens with base 10.

    ...

    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.

    From Why hash tables should use a prime-number size.

提交回复
热议问题