Efficient computation of ratios between divisor sums

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, σ\sigma, and its relation to bitwise operations. Specifically I show that

rσ(k,n)=σ(2kn)σ(n)=xor(2kn,2kn1)xor(n,n1) where kNr_\sigma(k,n)=\frac{\sigma(2^kn)}{\sigma(n)}=\frac{xor(2^kn, 2^kn-1)}{xor(n,n-1)}\text{ where }k \in \mathbb{N}

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!

Divisor sum

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 σ(n)\sigma(n) to denote the sum of divisors of nn. The divisor sum is exactly what it sounds like, sum up all divisors of a number inclusive itself and one.

σ(1)=1=1\sigma(1)=1=1
σ(2)=2+1=3\sigma(2)=2+1=3
σ(3)=3+1=4\sigma(3)=3+1=4
σ(4)=4+2+1=7\sigma(4)=4+2+1=7
σ(5)=5+1=6\sigma(5)=5+1=6
σ(6)=6+3+2+1=12\sigma(6)=6+3+2+1=12
σ(7)=7+1=8\sigma(7)=7+1=8
σ(8)=8+4+2+1=15\sigma(8)=8+4+2+1=15

We will examine ratios between divisor sums such as if we know σ(n)\sigma(n) can we directly calculate σ(an)\sigma(an) without explicitly summing divisors? It turns out we can, at least for any a=2ka=2^k.

Highest power of 2 dividing n

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 P2(n)P_2(n) be the highest power of two dividing n.

Odd numbers doesn’t have any 2 as divisor, therefore every odd nn in the sequence is 1. Some examples

P2(3)=20=1P_2(3)=2^0=1
P2(52)=21=2P_2(5\cdot2)=2^1=2
P2(322)=22=4P_2(3\cdot2\cdot2)=2^2=4
P2(2222)=24=16P_2(2\cdot2\cdot2\cdot2)=2^4=16

Since the binary system uses the power of two, we can find P2(n)P_2(n) using the bitwise trick

P2(n)=and(n,n)P_2(n)=and(n,-n)

This works on systems that use two’s complement to represent negative numbers. The two’s complement n=2BITSn*n=2^{BITS}-n, or equivalently n=n~+1*n=\tilde n+1, where n~\tilde n is bitwise negation (flip all the bits).

After negation, the bits of nn and n~\tilde n are disjoint. Then n~+1\tilde n + 1 will cause 1 bit overlap at the lowest zero of n~\tilde n (the binary addition will negate any 1’s on the way) - this is the lowest 1 in nn which represents the highest power of 2 dividing nn. The and operation will mask this bit out.

Example, using 8 bits and n=28n=28, the overlapping boxed lowest bit gets masked out

op binary
nn 00011100200011\fbox100_2
n~\tilde n 11100011211100011_2
n=n~+1-n=\tilde n + 1 11100100211100\fbox100_2
and(n,n)and(n,-n) 00000100200000100_2

Another way of calculating P2(n)P_2(n) without taking the complement is to use xorxor

P2(n)=xor(n,n1)+12P_2(n)=\frac{xor(n, n-1)+1}{2}

We use the same example, n=28n=28 to get a feel of how it works

op binary
nn 00011100200011100_2
n1n-1 00011011200011011_2
xor(n,n1)xor(n,n-1) 00000111200000111_2
xor(n,n1)+1xor(n,n-1)+1 00001000200001000_2
xor(n,n1)+12\frac{xor(n,n-1)+1}{2} 00000100200000100_2

Using xorxor requires a few more operations than andand but the reason I’m mentioning it is because it comes in very handy when we calculate the divisor sum of P2(n)P_2(n).

As a note, 2kP2(n)=P2(2kn)2^kP_2(n) = P_2(2^kn) 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 kk steps before or after we find the highest power of 2 dividing nn (lowest set bit) - in both cases it has moved kk steps to the left.

Since P2(n)P_2(n) is a power of two, taking log2(P2(n))=log2(2k)=klog_2(P_2(n))=log_2(2^k)=k will give us the exponent, kk of the highest power dividing n.

Now let’s connect this with the divisor sum starting with σ(P2(n))\sigma(P_2(n)) and the example, σ(16)=1+2+4+8+16=251\sigma(16)=1+2+4+8+16=2^5-1

The sum of all power of two divisors up and including P2(n)P_2(n) equals 2log2(P2(n))+11=22log2(P2(n))1=2P2(n)12^{log_2(P_2(n))+1}-1=2\cdot2^{log_2(P_2(n))}-1=2\cdot P_2(n)-1.

σ(P2(n))=2P2(n)1\sigma(P_2(n))=2P_2(n)-1

Finally recalling the relationship between P2(n)P_2(n) and xorxor we conveniently arrive at

σ(P2(n))=xor(n,n1)\sigma(P_2(n))=xor(n,n-1)

For example σ(16)=16+8+4+2+1=2161=xor(16,15)=31\sigma(16)=16+8+4+2+1=2\cdot16-1=xor(16, 15)=31. See the binary example above to understand why this work, after the xorxor operation all bits are set up until, and including, the highest power of 2 dividing nn - which equal the sum of divisors.

Ratios between divisor sums

Using the given identities we can effectively find ratios between divisor sums as long as the ratios between nn is a power of two. For example the ratio between the nn in σ(2n)σ(n)\frac{\sigma(2n)}{\sigma(n)} is 212^1, σ(4n)σ(n)\frac{\sigma(4n)}{\sigma(n)} is 222^2 and also for negative powers, σ(n)σ(8n)\frac{\sigma(n)}{\sigma(8n)} where the ratio is 232^{-3}.

Lets introduce a new function, the sigma ratio function

rσ(k,n)=σ(2kn)σ(n) where kNr_\sigma(k,n)=\frac{\sigma(2^kn)}{\sigma(n)}\text{ where }k \in \mathbb{N}

What is pretty neat is that we can calculate rσ(k,n)r_\sigma(k,n) without involvement of the traditional σ\sigma function. Instead we can solely rely on the computationally cheap bitwise operation xorxor.

Let’s start with an example for k=1k=1 and n=28n=28.

rσ(1,28)=σ(2128)σ(28)=σ(237)σ(227)=σ(23)σ(7)σ(22)σ(7)=σ(23)σ(22)=157r_\sigma(1, 28)=\frac{\sigma(2^1 28)}{\sigma(28)}=\frac{\sigma(2^3 7)}{\sigma(2^2 7)}=\frac{\sigma(2^3)\sigma(7)}{\sigma(2^2)\sigma(7)}=\frac{\sigma(2^3)}{\sigma(2^2)}=\frac{15}{7}

Since the σ\sigma function is multiplicative we may cancel the part that is not a power of two, σ(7)\sigma(7) and we are only left with the divisor sum of power of twos. We can generalize this as

rσ(k,n)=σ(2kn)σ(n)=σ(2kP2(n))σ(P2(n))=σ(P2(2kn))σ(P2(n))r_\sigma(k,n)=\frac{\sigma(2^kn)}{\sigma(n)}=\frac{\sigma(2^k P_2(n))}{σ(P_2​(n))}=\frac{\sigma(P_2(2^kn))}{σ(P_2​(n))}

Recall from the previous section that

σ(P2(n))=2P2(n)1=xor(n,n1)\sigma(P_2(n))=2P_2(n)-1=xor(n,n-1) Substituting this yields our final form

rσ(k,n)=xor(2kn,2kn1)xor(n,n1) where kNr_\sigma(k, n)=\frac{xor(2^kn, 2^kn-1)}{xor(n,n-1)}\text{ where }k \in \mathbb{N}

Application

Lets try with k=2k=2 which is a ratio of 44

nn σ(n)\sigma(n) σ(4n)\sigma(4n) rσ(2,n)r_\sigma(2, n)
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 σ(2kn)\sigma(2^kn) if you already know σ(n)\sigma(n), if so you just rearrange σ(2kn)σ(n)=rσ(k,n)\frac{\sigma(2^kn)}{\sigma(n)}=r_\sigma(k, n) into

σ(2kn)=σ(n)rσ(k,n)\sigma(2^kn)=\sigma(n)\cdot r_\sigma(k, n)

Identities

Here is a small collection of some bitwise identities that may be helpful.

identity oeis sequence
and(n,n)and(n,-n) A006519 Highest power of 2 dividing n. 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16
log2(and(n,n))log_2(and(n,-n)) 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
log2(and(n,n))+1log2​(and(n,−n))+1 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
2and(n,n)=xor(n,n)2and(n,-n)=-xor(n,-n) 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
σ(and(n,n))=2and(n,n)1=xor(n,n1)σ(and(n, -n))=2and(n,-n)-1=xor(n, n-1) 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

Sequences with ratios

ratio numerator denominator
σ(2n)σ(n)\frac{\sigma(2n)}{\sigma(n)} 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
σ(4n)σ(n)\frac{\sigma(4n)}{\sigma(n)} 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
σ(8n2)σ(4n2)\frac{\sigma(8n^2)}{\sigma(4n^2)} 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.