Given Prime Number N, Compute the Next Prime?

后端 未结 9 1767
忘掉有多难
忘掉有多难 2020-11-29 17:41

A coworker just told me that the C# Dictionary collection resizes by prime numbers for arcane reasons relating to hashing. And my immediate question was, \"how does it know

9条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 17:57

    As far as I remember, it uses prime number next to the double of current size. It doesn't calculate that prime number - there table with preloaded numbers up to some big value (do not exactly, something about around 10,000,000). When that number is reached, it uses some naive algorithm to get next number (like curNum=curNum+1) and validates it using some if these methods: http://en.wikipedia.org/wiki/Prime_number#Verifying_primality

提交回复
热议问题