Given Prime Number N, Compute the Next Prime?

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

    There's no function f(n) to calculate the next prime number. Instead a number must be tested for primality.

    It is also very useful, when finding the nth prime number, to already know all prime numbers from the 1st up to (n-1)th, because those are the only numbers that need to be tested as factors.

    As a result of these reasons, I would not be surprised if there is a precalculated set of large prime numbers. It doesn't really make sense to me if certain primes needed to be recalculated repeatedly.

提交回复
热议问题