hashtable size and significant bits of key

孤街醉人 提交于 2019-12-24 03:09:40

问题


I have a question regarding hashtable size and modular hashing. The hashing algorithm I'm referring to is the following: hash_key % table_size = array_index. I'm reading an algorithms textbook where the following bit of advice is given:

If the table size is not prime, it may be the case that all of the bits of the key do not play a role in determining the array_index.

Can anyone explain what this means exactly with an example?


回答1:


What you want to avoid is common factors. There is a theorem that states that every number can be expressed as a product of prime numbers.

So as a consequence if you have a prime number as a mod. You will not share any factors in the division.

say A % 30, so any multiple of 2, 3 and 5 will share the factors in the division and that factor will be useless in the division.

250/30 = 50 / 6 = 25 / 3

You want to minimize useless factors.



来源:https://stackoverflow.com/questions/10358712/hashtable-size-and-significant-bits-of-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!