![]() |
Number of set bits in multiple
I have a new strange problem:
Given a number "m", let's say random, thus has equal probability for its bits (in binary representation) of being 0 or 1. So in average it has log2(m)/2 1-bits. How can I produce a multiple of m that has significantly less 1-bits in its binary representation? The slow way would be to iterate over multiples m*k (for k in [1..N]), and select the one with the lowest number of set bits. But it seems to me that this produces a reduction of about log2(N) 1-bits, thus this method is inefficient. Do you know a trick for getting a multiple with a low count of set-bits? Thanks! |
You only need to consider odd multipliers.
You can also consider multipliers of the form Mod(m, 2^n)^-1 (for this to work, m should be odd -- but that shouldn't be a problem). |
[QUOTE=preda;494844]
Do you know a trick for getting a multiple with a low count of set-bits? Thanks![/QUOTE] Avoid power of two multiples, is one trick, they all have the same as the original number. |
[QUOTE=axn;494849]You only need to consider odd multipliers.
You can also consider multipliers of the form Mod(m, 2^n)^-1 (for this to work, m should be odd -- but that shouldn't be a problem).[/QUOTE] Yes, "m" is odd, and yes, only odd multipliers considered. By [QUOTE]Mod(m, 2^n)^-1[/QUOTE], you mean: find x such that (m * x) % 2^n == 1 ? But if such x has about n bits, then this technique just shifts the 1-bits up, but does not necessarily remove them; so how does this help? |
[QUOTE=preda;494852]Yes, "m" is odd, and yes, only odd multipliers considered.
By , you mean: find x such that (m * x) % 2^n == 1 ? But if such x has about n bits, then this technique just shifts the 1-bits up, but does not necessarily remove them; so how does this help?[/QUOTE] You can vary n from 1 .. bitcount(m). The multiplier has the property that LSB of the product is a pattern 0000...001 (n-1 zeroes and a 1). You still have to search and see if something gives a good result; there are no guarantees. You can combine the two approaches and only search multipliers whose lowest n bits are the result of m^-1 (mod 2^n). n=1 is just all the odd multipliers, so you'll have to pick a higher one -- not sure how to go about selecting an n. |
[QUOTE=axn;494856]You can vary n from 1 .. bitcount(m). The multiplier has the property that LSB of the product is a pattern 0000...001 (n-1 zeroes and a 1). You still have to search and see if something gives a good result; there are no guarantees.
You can combine the two approaches and only search multipliers whose lowest n bits are the result of m^-1 (mod 2^n). n=1 is just all the odd multipliers, so you'll have to pick a higher one -- not sure how to go about selecting an n.[/QUOTE] Yes. It's just that I don't want to search "systematically" because it's too slow, let me explain. Let's say "m" has 1M bits, with 50% 1-bits in average. Let's say I want to reduces the number of 1-bits from 50% to 25%, that would mean I need a reduction of about 250K bits. Now, choosing the better between 2 multipliers reduces 1bit. For each additional bit, I need to double the search space. So to cut about 250K bits I'd need to check 2^250K multipliers, not practical. I was thinking if anything more direct can be used. |
[QUOTE=preda;494844]I have a new strange problem:
Given a number "m", let's say random, thus has equal probability for its bits (in binary representation) of being 0 or 1. So in average it has log2(m)/2 1-bits. How can I produce a multiple of m that has significantly less 1-bits in its binary representation? The slow way would be to iterate over multiples m*k (for k in [1..N]), and select the one with the lowest number of set bits. But it seems to me that this produces a reduction of about log2(N) 1-bits, thus this method is inefficient. [/QUOTE] Basically the real reduction for a "random" m value is much larger, fix say N=1024, then by large probability you could reduce the number of bits by sqrt(log2(m)) in k*m. I see that this is compression problem and it is a hopeless effort to reach even a tiny compression for (general) large numbers, see [url]https://en.wikipedia.org/wiki/Kolmogorov_complexity[/url] (compression section). Ofcourse for special numbers we can reach a great compression, for example for n=2^p-1. |
[QUOTE=R. Gerbicz;494859]Basically the real reduction for a "random" m value is much larger, fix say N=1024, then by large probability you could reduce the number of bits by sqrt(log2(m)) in k*m.
[/QUOTE] I don't understand. Could you explain in more detail please? [QUOTE] I see that this is compression problem and it is a hopeless effort to reach even a tiny compression for (general) large numbers, see [url]https://en.wikipedia.org/wiki/Kolmogorov_complexity[/url] (compression section). Ofcourse for special numbers we can reach a great compression, for example for n=2^p-1. [/QUOTE] Yes, the compression view is useful, and shows the limits of what can be achieved. I think I can show that, for any "m", there exists a multiple that has at most 2 bits set, and this without contradicting the compression facts. The drawback is that the number of bits in such a number would be of the order of "m". |
[QUOTE=preda;494861]
I think I can show that, for any "m", there exists a multiple that has at most 2 bits set, and this without contradicting the compression facts. The drawback is that the number of bits in such a number would be of the order of "m".[/QUOTE] It goes like this: Given m, let's assume odd for simplicity. Let k = log2(m) +1, the number of bits in "m", Let d = 2^k % m, Let c = m - d, then 2^k + c is a multiple of m, and has nb. of 1-bits equal to 1 + nb-1-bits(c). Iterate: c(i) = (2^i * c) % m, this series eventually reaches 1. So there is a value p, where (2^p * c) % m == 1, and 2^(k+p) + 1 is a multiple of m. |
[QUOTE=preda;494861]I don't understand. Could you explain in more detail please?
[/QUOTE] In the range [0,2^L) the number of bits of integers follow a normal distribution with L/2 mean and with c*sqrt(L) variance. So most of the numbers has number of bits in [L/2-c1*sqrt(L),L/2+c1*sqrt(L)] interval, if we suppose that the same is true for k*m then with large probability we can gain a reduction by sqrt(L) bits. (reaching with small say k<1024 in most of the cases). [QUOTE=preda;494861] I think I can show that, for any "m", there exists a multiple that has at most 2 bits set.[/QUOTE] m=7 is the first counter-example, because there is no x for that 2^x+1==0 mod 7. |
[QUOTE=preda;494861]I think I can show that, for any "m", there exists a multiple that has at most 2 bits set[/QUOTE]
All multiples of 1048575 have at least 20 bits set. See [url]http://matwbn.icm.edu.pl/ksiazki/aa/aa38/aa3825.pdf[/url] [url]http://list.seqfan.eu/pipermail/seqfan/2009-December/003267.html[/url] [url=https://oeis.org/A005360]A005360[/url], [url=https://oeis.org/A125121]A125121[/url], [url=https://oeis.org/A086342]A086342[/url]. |
| All times are UTC. The time now is 18:41. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.