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

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

    I cannot prove it anymore, although I remember having to do so in an exam at university a million years ago, but optimal hash sizes are not merely prime. You want to pick a prime number N such that N = 4*M − 1 (where M is also an integer).

    That makes 31 a better number of buckets than 29. M is 8 when N is 31, but there is no integral M when N is 29.

    As I said, I no longer remember the math to prove this. It was in a theory course taught by Rachel Manber, Udi’s wife, about 25 years ago or so.

提交回复
热议问题