23

Out of curiosity, I am considering an interesting XOR "sum" of all reciprocals: $$X := 1 \oplus \frac{1}{2} \oplus \frac{1}{3} \oplus \frac{1}{4} \oplus \cdots= \bigoplus_{n=1}^{\infty} \frac{1}{n}$$ where $\oplus$ denotes bitwise XOR and all numbers $1/n$ are fractions represented in binary. The convention is that if a fraction has a terminating representation, we use it, i.e. $1/4$ is $0.01_2$ and not $0.00\overline{1}_2$ (although it would be interesting to analyze both). An example calculation of XORing fractions looks like this: $$\frac{1}{2} \oplus \frac{1}{3} = 0.1_2 \oplus 0.\overline{01}_2 = 0.11\overline{01}_2 = \frac{5}{6}.$$ which (coincidentally) matches regular addition. Another example: $$\frac{1}{5} \oplus \frac{1}{7} = 0.\overline{0011}_2 \oplus 0.\overline{001}_2$$

\begin{array} {|r|r|}\hline 0. & 0 & 0 & 1 & 1& 0 & 0 & 1 & 1 & 0 & 0 & 1 & 1 & \cdots\\ \hline \oplus\: 0. & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 & 1 & \cdots \\ \hline =0. & 0 & 0 & 0 & 1 & 0 & 1 & 1 & 1 & 1 & 0 & 1 & 0 & \cdots \\ \hline \end{array} $$\implies 0.\overline{0011}_2 \oplus 0.\overline{001}_2 = 0.\overline{000101111010}_2 = \frac{6}{65}.$$

I want to find the exact value of the "sum", $X$, or at least some more info about it.

My partial progress:

  • I know the "sum" converges: the $k$th bit in the result is uniquely determined by the first $2^k$ terms, with all terms after $2^k$ only affecting the next bits. This means that for a given $\epsilon > 0$, we can choose $N = 2^{-\log_2(\epsilon)} = \frac{1}{\epsilon}$. This proves the convergence of the partial sums and thus the convergence of the entire sum.
  • Numerical calculations show that $X \approx 1.539581$, however getting more digits is quite difficult. It takes $\mathcal{O}(2^n)$ time to calculate first $n$ binary digits. I am quite confident in the digits $1.10001010001000100000000_2$. Here's the Python program I used, using the binary_fractions package from pip:
from fractions import Fraction
from binary_fractions import Binary
N = 1000
res = Binary("0")
for i in range(1,N+1):
    res ^= Binary(Fraction(1,i))
print(res)
  • While I see no way to make the calculation have smaller time complexity, it can still be sped up more because XOR is commutative and associative, which is why I am planning on writing a multithreaded / GPU based version of this code which could split the workload on multiple threads and combine back together. However this is simply an effort to speed up the numerical computation instead of getting the closed form result (well, if there is one).
  • The problem can also be alternatively restated in trying to determine whether there is an even or an odd number of 1s in the binary representation of each fraction at some $k$th bit. If it is even, the resulting number would have a $0$ at the $k$th bit and $1$ otherwise.
  • Using the above alternative statement the problem can be generalized for base $b$ fractions by replacing XOR with addition mod $b$, but I have not investigated that (yet?).

So, my question is: has there been any research/literature on something like this? Is there a simple formula I am missing? I conjecture that it is irrational because there does not seem to be some periodic part but I do not know how to prove it.

EDIT 5/11/2024 My current progress includes these discoveries:

  • You actually need $2^{k-1}$ terms to determine the $k$th bit of $X$. All terms whose index is between $2^{k-1}$ and $2^k$ have their $k$th bit set to 1, and since there are $2^k - 2^{k-1} = 2^{k-1}$ terms in that interval, which is an even number, their total XOR becomes 0. So really, only the first $2^{k-1}$ matter.
  • An explicit formula for XOR of two fractions looks like this: if $$\frac{1}{A} = \sum_{k=1}^{\infty} a_k 2^{-k}\text{ and }\frac{1}{B} = \sum_{k=1}^{\infty} b_k 2^{-k}$$ where $a_k, b_k \in \{0,1\}$ are the bits of $1/A$ and $1/B$ respectively, $$\frac{1}{A} \oplus \frac{1}{B} = \sum_{k=1}^{\infty} (a_k + b_k - 2a_k b_k) 2^{-k} = \sum_{k=1}^{\infty} (a_k - b_k)^2 2^{-k} = \sum_{k=1}^{\infty} |a_k - b_k| 2^{-k}.$$
  • My C++ code for computing the value of $X$ can be found here: https://github.com/artemetra/binaryfrac. The current best value I have is $X \approx 0.10001010001000100000000000100000111100010000101100_2 \approx 1.539581306497932$. I am pretty certain in these digits.
  • With that being said, the main overarching problem here is then finding the patterns between the binary representations of reciprocals, which I haven't been able to tame much except from some very narrow edge cases (such as powers of 2 and terms around those).
artemetra
  • 598
  • Some values have two representations: Namely reciprocals of powers of $2$ have an expansion that terminates (or ends in all $0$s if you prefer) and one that ends in all $1$s. For instance $\frac14$ is equal to both $.01$ and $.00\overline{1}$ (in binary). Do you have a convention for which to use? (I believe it makes a difference in the calculation.) – paw88789 Oct 28 '24 at 11:54
  • 1
    I think you may need to say more about convergence. With the XOR sum, the partial 'sums' are not (necessarily) monotone. So simply invoking boundedness does not establish convergence. – paw88789 Oct 28 '24 at 11:57
  • @paw88789 It does have a difference, you are right, I'll mention it in the post. All fractions whose representation can terminate do terminate. – artemetra Oct 28 '24 at 12:10
  • @paw88789 You are also right about your comment on convergence. I'll add my reasoning to the post as well. – artemetra Oct 28 '24 at 12:14

2 Answers2

3

It's possible to compute these bits extremely rapidly, using the "alternate restatement" mentioned in the original problem. Indeed, let $B_k$ be the number of elements of $\{1, 1/2, 1/3, 1/4, \ldots\}$ with a $1$ in their $k$-th binary bit, modulo $2$. Recall that the $k$-th binary bit of any $x\in\mathbb{R}_{+}$ (after the decimal point) is given by $\lfloor{2^k x}\rfloor$ mod $2$. So we want $$ B_k=\left(\sum_{n=1}^{\infty}\left\lfloor\frac{2^k}{n}\right\rfloor {\text{ mod }} 2\right){\text{ mod }}2=A(2^k){\text{ mod }}2, $$ where we define $$ A(x) \equiv \sum_{n=1}^{x}\left\lfloor\frac{x}{n}\right\rfloor. $$ The values in the sum begin to repeat themselves when $n$ becomes large. Generally, $\lfloor{x/n}\rfloor = \ell$ exactly when $1+\lfloor{x/(\ell+1)}\rfloor \le n \le \lfloor{x/\ell}\rfloor$. So for any choice of cutoff $\ell_{0}$, the sum can be rewritten as: $$ A(x)=\sum_{n=1}^{\lfloor{x/(\ell_0+1)}\rfloor}\left\lfloor\frac{x}{n}\right\rfloor + \sum_{n=1+\lfloor{x/(\ell_0+1)}\rfloor}^{x}\left\lfloor\frac{x}{n}\right\rfloor=\sum_{n=1}^{\lfloor{x/(\ell_0+1)}\rfloor}\left\lfloor\frac{x}{n}\right\rfloor+\sum_{\ell=1}^{\ell_0}\left(\left\lfloor\frac{x}{\ell}\right\rfloor - \left\lfloor\frac{x}{\ell+1}\right\rfloor\right)\ell. $$ The second sum actually telescopes into a form similar to the first, giving $$ A(x)=\sum_{n=1}^{\lfloor{x/(\ell_0+1)}\rfloor}\left\lfloor\frac{x}{n}\right\rfloor + \sum_{\ell=1}^{\ell_0}\left\lfloor\frac{x}{\ell}\right\rfloor - \ell_0 \left\lfloor\frac{x}{\ell_0+1}\right\rfloor. $$ This is quite remarkable, in that the first and second sums have the same form... and what's more, for choices of $\ell_0$ that are $\approx \sqrt{x}$, they will differ only by a small number of terms. Choose $\ell_0$ small enough that $\ell_0 + 1 \le \lfloor{x / (\ell_0 + 1)}\rfloor$, but otherwise as large as possible. Then $$ A(x)=2\sum_{\ell=1}^{\ell_0}\left\lfloor\frac{x}{\ell}\right\rfloor + \sum_{\ell=\ell_0+1}^{\lfloor{x / (\ell_0 + 1)}\rfloor}\left\lfloor\frac{x}{\ell}\right\rfloor - \ell_0 \left\lfloor\frac{x}{\ell_0+1}\right\rfloor. $$ But to calculate the bits $B_k$, we only need this modulo $2$, and so the first term -- the only term that is slow to calculate! -- can be discarded. The result is $$ B_k=A(2^k){\text{ mod }} 2 = \left( \sum_{\ell=\ell_0+1}^{\lfloor{2^k / (\ell_0 + 1)}\rfloor}\left\lfloor\frac{2^k}{\ell}\right\rfloor - \ell_0 \left\lfloor\frac{2^k}{\ell_0+1}\right\rfloor \right) {\text{ mod }} 2. $$ This can all be done using integer arithmetic, with a bisection search to find the best value of $\ell_0$. The algorithm can be written in a dozen lines of Python code, and calculates the first $1000$ bits in less than a second.

mjqxxxx
  • 43,344
  • 1
    I should note that my calculated bits only agree with yours up to about the $33$rd. After that, they diverge. I find the first hundred bits to be: $1000101000100010000000000010000010101010000010100000101000001010101010101000001010100010101010000010$. If these are correct, then there is a simple structure... these are the binary digits of $\sqrt{2}$ alternating with zeroes. – mjqxxxx Nov 07 '24 at 00:16
  • 1
    Note also that the expression I gave implies that $B_k=0$ for even $k$. – mjqxxxx Nov 07 '24 at 00:26
  • 2
    So... I won't change the answer I wrote, but the result can be expressed much more simply. It's just that $A(x)=\sum_{n=1}^{\infty} \lfloor x / n \rfloor \equiv \lfloor \sqrt{x} \rfloor$ (mod $2$). Since $\sqrt{x}$ is an even integer when $x=2^k$ for even $k$, those bits are zero. For odd $k$, $\sqrt{2^k}$ is $\sqrt{2}$ times a power of $2$; so those bits are equal to the digits of $\sqrt{2}$. – mjqxxxx Nov 07 '24 at 03:57
  • This is really cool! Your definition of B_k has mod 2 inside the sum (i assume), but that's not included in A_k - why? – artemetra Nov 07 '24 at 09:21
  • Oh, just because the two applications of mod 2 (one for finding the $k$-th digit, and one for the XOR) can be combined into one. – mjqxxxx Nov 07 '24 at 14:52
  • In writing my own python code I've noticed that since we require $\ell_0 + 1 \le \lfloor{x / (\ell_0 + 1)}\rfloor$ with $\ell_0$ being as large as possible, the sum $\sum_{\ell=\ell_0+1}^{\lfloor{x / (\ell_0 + 1)}\rfloor}\left\lfloor\frac{x}{\ell}\right\rfloor$ is always small in comparison to the $- \ell_0 \left\lfloor\frac{x}{\ell_0+1}\right\rfloor$ part, making the value of A(x) large and negative. Is that intended? Could you share your code? – artemetra Nov 08 '24 at 10:55
1

I did a quick look on OEIS using your calculated digits as the sequence and got nothing that was immediately identifying. I also calculated the count of 1s in each position (which you could then transform to the bitwise XOR value by just taking those results modulo 2) and checked it in OEIS but nothing came up.

For reference, here was my approach using probably quite poor Python:

from fractions import Fraction
from binary_fractions import Binary

def reciprocal_binary(n): if n == 1: bdigs = '1' else: n_recip = Binary(Fraction(1, n)) bdigs = '0' + str(n_recip).split(".")[1] bdigs = bdigs.ljust(128, '0') return [int(d) for d in bdigs]

digit_array = []

for n in range(2 ** 16): digit_array.append(list(reciprocal_binary(n + 1)))

digit_sum = [sum(x) for x in zip(*digit_array)]

Which gives counts of 1, 1, 2, 4, 10, 19, 42, 85, 176, 348, 702, 1413, 2846, 5668, 11336, ... corresponding to the first few digits of $X$ being 1.10001010001000.

As for whether you can explicitly calculate $X$, here is a partial approach:

This paper, if you have a natural number $N$ that can be written in binary as $N = \sum_{i = 0}^{k - 1} a_i 2^i$, and specifically assuming that $a_0 = a_{k-1} = 1$ (i.e. $N$ is odd and it has $k$ binary digits), then if you take $b_0 = a_0 = 1$ and $b_i = 1 - a_i$ for $1 \leq i < k$, you can express the reciprocal of $N$ as

$$\frac{1}{N} = \frac{1}{2^k} \sum_{j = 0}^\infty \left(\sum_{i = 0}^{k - 1} b_i 2^{i - k}\right)^j$$

which is a geometric series of terms based on the twos complement of $N$. If $N$ is even, then you just factor out all of the 2s and adjust the reciprocal accordingly.

The paper also discusses algorithms for efficiently finding the binary expansion of $\frac{1}{N}$. Using the theorems and algorithms in the paper, you can probably do some manipulations to get expressions for the count of 1s in each position across all the reciprocals, and hence the bitwise XOR. Whether finding this value is easy is another matter - there's a good chance you have to delve into some number theory and look at prime factorizations or the like.

With that said, I do think it's an interesting enough sequence that you could look at submitting it to OEIS, both as counts and as binary digits.

ConMan
  • 27,579