mersenneforum.org  

Go Back   mersenneforum.org > Great Internet Mersenne Prime Search > Math

Reply
 
Thread Tools
Old 2018-08-29, 08:39   #1
preda
 
preda's Avatar
 
"Mihai Preda"
Apr 2015

25338 Posts
Default 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!
preda is offline   Reply With Quote
Old 2018-08-29, 09:54   #2
axn
 
axn's Avatar
 
Jun 2003

5,051 Posts
Default

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).
axn is offline   Reply With Quote
Old 2018-08-29, 10:02   #3
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by preda View Post

Do you know a trick for getting a multiple with a low count of set-bits?

Thanks!
Avoid power of two multiples, is one trick, they all have the same as the original number.
science_man_88 is offline   Reply With Quote
Old 2018-08-29, 10:26   #4
preda
 
preda's Avatar
 
"Mihai Preda"
Apr 2015

3·457 Posts
Default

Quote:
Originally Posted by axn View Post
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).
Yes, "m" is odd, and yes, only odd multipliers considered.

By
Quote:
Mod(m, 2^n)^-1
, 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?
preda is offline   Reply With Quote
Old 2018-08-29, 11:00   #5
axn
 
axn's Avatar
 
Jun 2003

10011101110112 Posts
Default

Quote:
Originally Posted by preda View Post
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?
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.
axn is offline   Reply With Quote
Old 2018-08-29, 11:25   #6
preda
 
preda's Avatar
 
"Mihai Preda"
Apr 2015

3·457 Posts
Default

Quote:
Originally Posted by axn View Post
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.
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.
preda is offline   Reply With Quote
Old 2018-08-29, 12:07   #7
R. Gerbicz
 
R. Gerbicz's Avatar
 
"Robert Gerbicz"
Oct 2005
Hungary

101110011002 Posts
Default

Quote:
Originally Posted by preda View Post
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.
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 https://en.wikipedia.org/wiki/Kolmogorov_complexity (compression section). Ofcourse for special numbers we can reach a great compression, for example for n=2^p-1.

Last fiddled with by R. Gerbicz on 2018-08-29 at 12:09 Reason: corrected formula
R. Gerbicz is offline   Reply With Quote
Old 2018-08-29, 13:25   #8
preda
 
preda's Avatar
 
"Mihai Preda"
Apr 2015

3×457 Posts
Default

Quote:
Originally Posted by R. Gerbicz View Post
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 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 https://en.wikipedia.org/wiki/Kolmogorov_complexity (compression section). Ofcourse for special numbers we can reach a great compression, for example for n=2^p-1.
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".
preda is offline   Reply With Quote
Old 2018-08-29, 13:37   #9
preda
 
preda's Avatar
 
"Mihai Preda"
Apr 2015

25338 Posts
Default

Quote:
Originally Posted by preda View Post
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".
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.
preda is offline   Reply With Quote
Old 2018-08-29, 14:26   #10
R. Gerbicz
 
R. Gerbicz's Avatar
 
"Robert Gerbicz"
Oct 2005
Hungary

22·7·53 Posts
Default

Quote:
Originally Posted by preda View Post
I don't understand. Could you explain in more detail please?
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:
Originally Posted by preda View Post
I think I can show that, for any "m", there exists a multiple that has at most 2 bits set.
m=7 is the first counter-example, because there is no x for that 2^x+1==0 mod 7.

Last fiddled with by R. Gerbicz on 2018-08-29 at 14:28
R. Gerbicz is offline   Reply With Quote
Old 2018-08-29, 14:52   #11
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by preda View Post
I think I can show that, for any "m", there exists a multiple that has at most 2 bits set
All multiples of 1048575 have at least 20 bits set. See
http://matwbn.icm.edu.pl/ksiazki/aa/aa38/aa3825.pdf
http://list.seqfan.eu/pipermail/seqf...er/003267.html
A005360, A125121, A086342.
CRGreathouse is offline   Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using multiple PCs numbercruncher Information & Answers 18 2014-04-17 00:17
64 bits versus 32 bits Windows S485122 Software 2 2006-10-31 19:14
35-35.2 to 62 bits, cont from 61 bits Khemikal796 Lone Mersenne Hunters 12 2005-12-01 21:35
Testing same number on multiple machines dotnet_dev Software 17 2003-06-16 14:30
Multiple systems/multiple CPUs. Best configuration? BillW Software 1 2003-01-21 20:11

All times are UTC. The time now is 18:43.


Fri Jul 16 18:43:19 UTC 2021 up 49 days, 16:30, 1 user, load averages: 4.92, 5.27, 4.72

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.