It is possible to do this in O(1) memory, for arbitrary sized collections, efficiently. The first trick is to make a function that can calculate the nth set with k elements from some universe U (in this case U = {0..2^5-1} without order (no duplicates) directly. This is done using the https://en.wikipedia.org/wiki/Combinatorial_number_system . This is very efficient. Then, to encode duplicates you use the stars and b…
Show HN: How to store a set of four 5-bit values in one 16-bit value
61–70 of 149 posts
Re: Show HN: How to store a set of four 5-bit values in one 16-bit value
#62Earlier quoted context omitted.
To answer the obvious next question: Four 5-bit values in order have 20 bits of entropy, so cannot be stored in 16 bits. Four 5-bit values without order have 20 - log2(4!) =~ 20 - 4.59 = 15.41 bits of entropy (corresponding to log(2^20/4!) possible configurations), and thus can fit in 16 bits of data if you're clever about it.
Your math is wrong. If two of the numbers happen to be the same, order no longer matters for those numbers, so your log2(4!) needs to be larger...
If you want to optimize storage of duplicates, you still have to store the number of duplicate numbers, and you are back where you started.
Re: Show HN: How to store a set of four 5-bit values in one 16-bit value
#63Earlier quoted context omitted.
Yes, you're missing something. Radix sort, like most sorting algorithms, requires storing the intermediate results in memory. This problem seems impossible at first blush because storing 1 million 4-byte integers would seem to require 4M of RAM, and only 2M is available.
But if I pick my buckets right, I can store only the lsb in each bucket. Or more efficient, dynamically construct an implicit trie/heap type thing, where each leaf is the count of times that int appears (and the int itself is encoded in the location) That feels really, really handwavy but promising?
Re: Show HN: How to store a set of four 5-bit values in one 16-bit value
#64It is possible to do this in O(1) memory, for arbitrary sized collections, efficiently. The first trick is to make a function that can calculate the nth set with k elements from some universe U (in this case U = {0..2^5-1} without order (no duplicates) directly. This is done using the https://en.wikipedia.org/wiki/Combinatorial_number_system . This is very efficient. Then, to encode duplicates you use the stars and b…
Can you elaborate on the second part? Are you saying you're going to expand the size of the universe by a factor of 2^(k-1) to use as locations of the "bars"?
Re: Show HN: How to store a set of four 5-bit values in one 16-bit value
#65Ah, without preserving order. That's a set of four 5-bit values, not a sequence of four 5-bit values.
Re: Show HN: How to store a set of four 5-bit values in one 16-bit value
#66how are you getting 3876 unique values for a set of 4 4 bit values (16, 16, 16, 16) ?
https://en.wikipedia.org/wiki/Bag_(mathematics)#Counting_mul...
Re: Show HN: How to store a set of four 5-bit values in one 16-bit value
#67Re: Show HN: How to store a set of four 5-bit values in one 16-bit value
#68Re: Show HN: How to store a set of four 5-bit values in one 16-bit value
#69It is possible to do this in O(1) memory, for arbitrary sized collections, efficiently. The first trick is to make a function that can calculate the nth set with k elements from some universe U (in this case U = {0..2^5-1} without order (no duplicates) directly. This is done using the https://en.wikipedia.org/wiki/Combinatorial_number_system . This is very efficient. Then, to encode duplicates you use the stars and b…
Re: Show HN: How to store a set of four 5-bit values in one 16-bit value
#70This is quite related to the problem of sorting a million 32 bit integers using only 2M of RAM (and no disk). It can be done.
So what is the trick?