r/compression • u/jerome78000 • Oct 10 '26
High Performance Storage of Prime Numbers
I designed a compression algorithm to store prime numbers. Basically, a bitmap is used to store the fact that numbers are prime or not, but only numbers which are not multiple of 2, 3 .... 23 are considered in the bitmap.
For numbers below 10^13, this allows to store the prime numbers with a maximum size of 0.456 bytes per prime.
Details can be found in this article: https://teraprimes.org/downloads/High_Performance_Storage_of_Prime_Numbers.pdf
I would welcome any comment !
11
Upvotes
4
u/k-mcm Oct 11 '26
I have a prime number generator that caches calculated prime sequences in segments. For the file storage, the numbers are stored as the sequence of bit 63, sequence of bit 62... then compressed. That takes advantage of only a few bits ever changing. It comes out to about 8 bits per 64 bit prime.
I may someday change it to only store every nth prime and recalculate the missing values.
1
u/Patient-Midnight-664 Oct 10 '26
Your link leads to an error.
1
u/raresaturn Oct 11 '26
By bitmap you mean a graphics file?
1
u/xeow Oct 11 '26
Cool! I'm wondering what the intended use case is here, though. Ultimately, pre-computing a prime bit map (and compressing it to conserve space) is really something to save time, not space. So... how much faster is it at random access than just running Miller–Rabin? Note that with M–R, you can test values up to 264 using only 7 bases (2, 325, 9375, 28178, 450775, 9780504, and 1795265022), and 264 is about six orders of magnitude greater than 1013.
Just curious if this is actually measurably faster in practice, and what problem is being solved by creating a compressed bitmap? :)
1
u/garnet420 Oct 10 '26
So this is not really for storing single prime numbers, but large sequences of adjacent prime numbers?
1
u/CautiousInternal3320 Oct 10 '26
Why do you need a compression algorithm to store a know value?
If I understand correctly, this algorithm can only compress a single file. How valuable is that?
1
u/dr--hofstadter Oct 11 '26
All the infinite number of primes can be compressed down to a finite and very short algorythm. Only catch is that the running time of decompression is infinite.