Given Prime Number N, Compute the Next Prime?

后端 未结 9 1783
忘掉有多难
忘掉有多难 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:56

    The gaps between consecutive prime numbers is known to be quite small, with the first gap of over 100 occurring for prime number 370261. That means that even a simple brute force will be fast enough in most circumstances, taking O(ln(p)*sqrt(p)) on average.

    For p=10,000 that's O(921) operations. Bearing in mind we'll be performing this once every O(ln(p)) insertion (roughly speaking), this is well within the constraints of most problems taking on the order of a millisecond on most modern hardware.

提交回复
热议问题