If I store in trivial way I can store roughly 5100 primes ie primes upto 50k in a 30KB file. Actually I need primes till $2^{30}$ but obviously its not possible to store such a huge list in a file of size of the order of some KBs. So my goal is the store as many primes as possible in a 30KB file. And then retrieve them in sublinear computation time. And beyond that limit I will have to sieve anyways. I have gone though this answer but doesnt quite fully satisfy me, So it would be great if someone can get the max compressed list with implementation of packing/unpacking algorithm.
I can afford to go for a retrieval/unpacking algo wchich is more than $O(1)$ per prime but has to be definitely sublinear ie less than $O(N)$ where N is size of list.
The simplest solution is to store a bitmask of which numbers are prime. This takes 1 bit per integer, which is enough to determine primality for all numbers up to roughly 2^18.
It is however, possible to do much better than this by not storing multiples of 2, 3, or 5. This will increase the size of the code a little bit, but will let you store 3.75x more data (or roughly up to 900,000).
This still isn't optimal since only roughly 1/3rd of these bits are 1s, which implies this data could be further compressed, but this is approaching the limit. I would be very surprised if it's possible to store anything more than the primes up to 2,000,000 or so.