Efficient storage of prime numbers

前端 未结 9 1849
深忆病人
深忆病人 2020-12-07 16:46

For a library, I need to store the first primes numbers up to a limit L. This collection must have a O(1) lookup time (to check whether a number is prime or not) and it must

9条回答
  •  春和景丽
    2020-12-07 17:34

    Given that memory is so cheap, I don't think you can do much better from a speed perspective than your existing scheme.

    If there's a better solution, then I'd assume it'd take advantage of the Prime Number Theorem that shows that as L gets bigger, the limit of

    π(L) / (L / ln(L)) approaches 1.

    Perhaps a better solution would have an adaptive packing solution in a data structure sort of like a skip list.

提交回复
热议问题