Jon Maiga, 2018-12-10
During the development of sequencedb.net I kept on encountering bitwise formulas that matched sequences involving ratios of divisor sums. I couldn’t fully understand why, so in this article I examine the ratio between the divisor sum, , and its relation to bitwise operations. Specifically I show that
To be clear, using bitwise operations for this purpose is not new (see oeis.org entries below), but I couldn’t find any good explanation or a general formula. So here goes!
Let’s start with a brief introduction to divisor sum.
A000203 a(n)=sigma(n), the sum of the divisors of n.
1, 3, 4, 7, 6, 12, 8, 15, 13, 18, 12, 28, 14, 24, 24, 31, 18, 39, 20, 42, 32, 36, 24, 60,
I’ll use the sigma sign to denote the sum of divisors of . The divisor sum is exactly what it sounds like, sum up all divisors of a number inclusive itself and one.
We will examine ratios between divisor sums such as if we know can we directly calculate without explicitly summing divisors? It turns out we can, at least for any .
Before we start looking on the ratios between divisor sums let’s establish some relations.
A006519 Highest power of 2 dividing n.
1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4,…
Let capital be the highest power of two dividing n.
Odd numbers doesn’t have any 2 as divisor, therefore every odd in the sequence is 1. Some examples
Since the binary system uses the power of two, we can find using the bitwise trick
This works on systems that use two’s complement to represent negative numbers. The two’s complement , or equivalently , where is bitwise negation (flip all the bits).
After negation, the bits of and are disjoint. Then will cause 1 bit overlap at the lowest zero of (the binary addition will negate any 1’s on the way) - this is the lowest 1 in which represents the highest power of 2 dividing . The and operation will mask this bit out.
Example, using 8 bits and , the overlapping boxed lowest bit gets masked out
op | binary |
---|---|
Another way of calculating without taking the complement is to use
We use the same example, to get a feel of how it works
op | binary |
---|---|
Using requires a few more operations than but the reason I’m mentioning it is because it comes in very handy when we calculate the divisor sum of .
As a note, holds. If we think of multiplication by two as left shifts (pretty common back in the days for optimization purposes), it doesn’t matter if we shift the lowest bit steps before or after we find the highest power of 2 dividing (lowest set bit) - in both cases it has moved steps to the left.
Since is a power of two, taking will give us the exponent, of the highest power dividing n.
Now let’s connect this with the divisor sum starting with and the example,
The sum of all power of two divisors up and including equals .
Finally recalling the relationship between and we conveniently arrive at
For example . See the binary example above to understand why this work, after the operation all bits are set up until, and including, the highest power of 2 dividing - which equal the sum of divisors.
Using the given identities we can effectively find ratios between divisor sums as long as the ratios between is a power of two. For example the ratio between the in is , is and also for negative powers, where the ratio is .
Lets introduce a new function, the sigma ratio function
What is pretty neat is that we can calculate without involvement of the traditional function. Instead we can solely rely on the computationally cheap bitwise operation .
Let’s start with an example for and .
Since the function is multiplicative we may cancel the part that is not a power of two, and we are only left with the divisor sum of power of twos. We can generalize this as
Recall from the previous section that
Substituting this yields our final form
Lets try with which is a ratio of
1 | 1 | 7 | 7/1 |
2 | 3 | 15 | 5/1 |
3 | 4 | 28 | 7/1 |
4 | 7 | 31 | 31/7 |
5 | 6 | 42 | 7/1 |
6 | 12 | 60 | 5/1 |
7 | 8 | 56 | 7/1 |
8 | 15 | 63 | 21/5 |
9 | 13 | 91 | 7/1 |
It works!
I am not sure what practical applications this formula may have but maybe it’s useful to be able quickly find if you already know , if so you just rearrange into
Here is a small collection of some bitwise identities that may be helpful.
identity | oeis | sequence |
---|---|---|
A006519 Highest power of 2 dividing n. | 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16 | |
A007814 Exponent of highest power of 2 dividing n | 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4 | |
A001511 The ruler function: 2^a(n) divides 2n. Or, a(n) = 2-adic valuation of 2n. | 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5 | |
A171977 a(n) = 2^(k+1) where 2^k is the highest power of 2 dividing n. | 2, 4, 2, 8, 2, 4, 2, 16, 2, 4, 2, 8, 2, 4, 2, 32 | |
A038712 Let k be the exponent of highest power of 2 dividing n | 1, 3, 1, 7, 1, 3, 1, 15, 1, 3, 1, 7, 1, 3, 1, 31 |
ratio | numerator | denominator |
---|---|---|
A088837 3, 7, 3, 15, 3, 7, 3, 31, 3, 7, 3, 15, 3, 7, 3, 63 | A038712 1, 3, 1, 7, 1, 3, 1, 15, 1, 3, 1, 7, 1, 3, 1, 31 | |
A088839 7, 5, 7, 31, 7, 5, 7, 21, 7, 5, 7, 31, 7, 5, 7, 127 | A088840 1, 1, 1, 7, 1, 1, 1, 5, 1, 1, 1, 7, 1, 1, 1, 31 | |
A065915 15, 63, 15, 255, 15, 63, 15, 1023, 15, 63, 15, 255, 15, 63, 15, 4095 | A065916 7, 31, 7, 127, 7, 31, 7, 511, 7, 31, 7, 127, 7, 31, 7, 2047, |
Written with StackEdit.