Assuming that $x$ is a real number, the function $f\_n(x)$ is defined as follows: the value of $f\_n(x)$ is equal to the number of bits before the first occurrence of $n$ consecutive zero bits in the binary representation of the fractional part of $x$. For example, $$\begin{array}{l}
f\_1(0.11100110001\ldots) = f\_2(0.11100110001\ldots) = 3,\\
f\_2(0.1110110001\ldots) = f\_3(0.11100110001\ldots) = 6.
\end{array}$$


Here we can assume that if $n$ consecutive zero bits never occur in $x$, the value of $f\_n(x)$ is undefined: for example, $f\_n(\frac{1}{3}) = f\_n(0.010101\ldots\_2)$ is undefined for any $n>1$.


Let $r$ denote the real number such that $0 < r < 1$ and an $i$-th bit of the binary representation of the fractional part of $r$ is equal to the second-to-last bit of the binary representation of an $i$-th prime. That is, $$r = 0.11011001101001101011011000110\ldots\_2$$


There are $131007$ zero bits in the first $262144 = 2^{18}$ bits of the binary representation of the fractional part of $r$. The number of zeroes in the first $500000$ bits is $249888$.


Consider the value of $f\_5(r)=377$, which is significantly larger than expected in an unbiased sequence of random (or pseudo-random) bits: for comparison, $$f\_5(\pi)=95, f\_5(e)=89, f\_5(\sqrt{2})=7, f\_5(\sqrt{3})=92, f\_5(\sqrt{5})=53, f\_5(\sqrt{6})=25, f\_5(\sqrt{7})=115, f\_5(\sqrt{8})=6, f\_5(\sqrt{10})=16.$$


Question: why is the value of $f\_5(r)$ so large?



Explanation of unexpectedly large offset of the first occurrence of five consecutive zeroes in the sequence of second-to-last bits of primes
I don't think this is particularly surprising. In particular, it seems like it's a 1-in-400 event or so. Mathematics is big enough that there are [lots of rare events](https://www.maa.org/sites/default/files/pdf/upload_library/22/Ford/Guy697-712.pdf)!


Let's first think about the simpler question of asking when we expect to first see a sequence of five zeroes in a sequence of Bernoulli ($p = 1/2$) random variates. You might expect that this random variate - the one counting the first sequence position at which we see five zeroes; let's call it $S$ - satisfies $S \thicksim \mathrm{Geometric}(1/32)$, in which case $\Pr(S \ge 377) \approx 6 \times 10^{-6}$ - a very unlikely event indeed.


This belief, however, is false! The key point is that appearances of five zeroes are very not independent, and this lack of independence makes the tails for our sequence behave very differently. Here are two heuristic explanations for this lack of independence:


* **Disjoint runs of zeroes repel one another.** This is just because runs of zeroes have to be separated by ones.
* **Long runs of zeroes cluster.** If I know that there is a run of five zeroes starting at position $i$, then the probability that there is a run of five zeroes starting at position $i+1$ is $1/2$ - far greater than the $1/32$ expected by chance.


A nice rule-of-thumb for runs in this particular process is that $S$ is approximately distributed like $2\cdot\mathrm{Geometric(1/32)}$. I think the expected value of $S$ is actually 62; in general, the expected waiting time before $n$ zeroes is $2^{n+1}-2$. It's clear that the distribution should be geometric in the large scale, because the effects described above are short-range - in other words, the distribution of $S$ is memoryless at large scales. The constant $2$ is somewhat harder to explain easily; maybe somebody will provide a good explanation in the comments.


Assuming we buy this rule-of-thumb, then, we can compute $$\Pr(S \ge 377 | S \sim 2\mathrm{Geometric}(1/32)) \approx 2.5 \times 10^{-3} \approx \sqrt{6 \times 10^{-6}}$$ so that what you observed is a 1-in-400 event. This does not include possible contributions from phenomena like those described in John Omielan's comment.


You can look at Feller for more detailed analysis of runs like this; it's a classic topic in probability theory. A nice martingale-based approach to lots of similar questions, including surprising phenomena like the nontransitive [Penney's game](https://en.wikipedia.org/wiki/Penney%27s_game) (featured in a great Martin Gardner column!) and Conway's algorithm for analyzing this game, is found in [Li (1980)](https://www.jstor.org/stable/2243018).


