Efficient storage of prime numbers

前端 未结 9 1875
深忆病人
深忆病人 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:25

    If you can figure out which ones are Mersenne or other easily represented prime numbers, you might be able to save a few bits by using that representation with a flag for applicable numbers.

    Also, how about storing the numbers as the difference from the previous number? Then the size shouldn't rise quite as fast (but lookup would be slow). Combining with the approach above, you could store Mersenne primes and the difference from the last Mersenne prime.

提交回复
热议问题