1

Can take the $8$ bits with the $\ge 4$ contiguous bits as a block. Need consider the different cases separately.
The positions possible for each of the size of blocks is:
(i) $4$ contiguous bits : $^8P_4$,
(ii) $5$ contiguous bits : $^8P_5$,
(iii) $6$ contiguous bits : $^8P_6$,
(iv) $7$ contiguous bits : $^8P_7$,
(v) $8$ contiguous bits : $^8P_8$,
Need to add all these cases by the addition principle, as these are mutually exclusive cases and comprise the solution space.

$=> ^8P_4 + ^8P_5 + ^8P_6 +^8P_7 + ^8P_8$

But, also need to consider the possible choices for each case. Also, in each case, except the last two cases above there is chance that two positions at the both sides of the contiguous block are $0$. I mean that it is possible that one end is the start position for the contiguous bits as $11110000$ which is different from $01111011$. For the case of $4$ contiguous bits, will the chances be :

$^8P_4\cdot(\,\,$case $00001111+$ case $11110000+$cases: $ (0/1)1111(0/1)(0/1)(0/1)\,\,) + $ cases$ (0/1)(0/1)(0/1)1111(0/1) \,\,)$

$=> ^8P_4\cdot(\,\, 1 + 1 + 2^4 +2^4)$

$=> ^8P_4\cdot(\,\, 2+ 2\cdot2^4)$

If the above analysis is correct for the case of $4$ contiguous bits, then how to generalize it for more bits.

======

Update :- My post considers $8$ separate positions, & then takes the $i=\{4,5,6,7,8\}$ possible contiguous $1$s. It takes the possible permutations of these all, then multiplies by the ways the other digits are chosen (i.e. $0,1$); & then adds all of these cases.

This is defective, as the $i$ contiguous $1$s are a unit, leaving only $5$ positions.
This is not obvious (to me), but can be checked by comparing the values of $^8P_4(=1680)$ to $^5P_4(=120)$.

Second, the $8-i$ positions can take a value based on where there are w.r.t. the contiguous block; i.e. if next to the block, then only one choice. This means that if the block is in a corner, then different number of choices are possible. So, need to consider individual cases.
This part is not a shortcoming of my answer, as have considered individual case separately.

Third, there is overlap between cases, as pointed out by the sole answer.

=======

Update 2 The python code for finding the sets' elements, & the intersection sets is given here, as provided by @SiongThyeGoh.

=========

Update 3 This is to have record of chats with @Siong Thye Goh :
(i) dt. 7th, 8th, 9th, 10th Oct., 2018; at: https://chat.stackexchange.com/transcript/84135/2018/10/7,
(ii) dt. 08 Nov. , 2018: https://chat.stackexchange.com/rooms/85486/jiten,
(iii) dt. 27 Nov. , 2018: https://chat.stackexchange.com/rooms/86261/8-bit-strings-having-4-contiguous,
(iv) dt. 17th Dec., 2018: https://chat.stackexchange.com/rooms/87190/stack-error-py3.

jiten
  • 4,962
  • 1
    The question is unclear. Are you asking for the probability that there is a block of at least consecutive ones in a bit string of length eight? If so, the cases of four consecutive ones and five consecutive ones are not mutually exclusive cases since a string of five consecutive ones contains two strings of four consecutive ones (starting with the first or second of the five consecutive bits). If I understand the problem correctly, you will need to use the Inclusion-Exclusion Principle or generating functions to solve the problem. – N. F. Taussig Oct 06 '18 at 22:49
  • @N.F.Taussig Yes, it is to get a block of at least $4$ contiguous (or, continuous) $1$s in a block of $8$ bits. Regarding, solving using the Inclusion-Exclusion Principle, or generating functions; am not clear. – jiten Oct 06 '18 at 22:52
  • 1
    Another option could be using a recurrence relation to count those bit strings with fewer than four consecutive ones, then subtracting that number from the total number of bit strings. – N. F. Taussig Oct 06 '18 at 23:00
  • @N.F.Taussig This should be a 3rd option. But, not clear about a generalized relation. – jiten Oct 06 '18 at 23:01
  • 1
    As a sanity check, you should compare your answer with the total number of bit strings of length eight. Since your answer exceeds that number, it cannot be correct. – N. F. Taussig Oct 06 '18 at 23:36
  • Thanks for showing that my answer is wrong, i.e. for the case of $4$ contiguous bits. I hope that the cause would be the number of combinations, rather than the number of permutations. Although, it is not clear why 'combinations' rather than 'permutations' fit the case; as ordering of the $4$ contiguous bits should matter. I have found that only $14$ possible bit strings are possible, which is not justifiable by taking $^8C_4$. These are: $00001111, 11110000, 01111000, 01111001, 01111010, 01111011, 00111100, 00111101, 10111100, 10111101, 00011110, 10011110, 01011110, 11011110$. – jiten Oct 07 '18 at 01:24
  • @N.F.Taussig I would request giving a combinatorics approach to justify the value $14$ (for $4$ contiguous $1$ bits) in my earlier comment, i.e. say take $5$ positions out of which $1$ position is fixed to be all $1$s. Still, seemingly no clear approach is possible. – jiten Oct 07 '18 at 01:37
  • There are $2^3$ with four ones in the first four positions since the fifth position must be a zero and we are free to choose a $0$ or $1$ in the last three positions. The other strings must be preceded and followed by a $0$, leaving two positions which may have a $0$ or a $1$. By symmetry, there are $2^3$ such strings with ones in the last four positions. Since the string $011110$ must begin in the first three positions, there are $3 \cdot 2^2$ such strings. That gives $2^3 + 2^3 + 3 \cdot 2^2 = 28$ bit strings of length $8$ with exactly four consecutive ones. – N. F. Taussig Apr 27 '23 at 13:09

6 Answers6

2

Since there are two ways to fill each position in a bit string, there are $2^8 = 256$ bit strings of length $8$. From these, we must subtract those bit strings with fewer than four consecutive ones.

Let $a_n$ denote the number of bit strings of length $n$ with fewer than four consecutive bit ones. Observe that any bit string of length one, two, or three has fewer than four consecutive ones and that the only bit string of length $4$ that contains four consecutive ones is $1111$. Hence, we have the initial conditions \begin{align*} a_1 & = 2\\ a_2 & = 4\\ a_3 & = 8\\ a_4 & = 15 \end{align*} A bit string with fewer than four consecutive ones can begin in one of four ways. They are $0$, $10$, $110$, and $1110$.

  • If an admissible bit string of length $n$ begins with $0$, the initial $0$ must be followed by an admissible bit string of length $n - 1$, of which there are $a_{n - 1}$.
  • If an admissible bit string of length $n$ begins with $10$, the initial string $10$ must be followed by an admissible bit string of length $n - 2$, of which there are $a_{n - 2}$.
  • If an admissible bit string of length $n$ begins with $110$, the initial string $110$ must be followed by an admissible bit string of length $n - 3$, of which there are $a_{n - 3}$.
  • If an admissible bit string of length $n$ begins with $1110$, the initial string $1110$ must be followed by an admissible bit string of length $n - 4$, of which there are $a_{n - 4}$.

Since these four cases are mutually exclusive and exhaustive, we have the recurrence relation $$a_n = a_{n - 1} + a_{n - 2} + a_{n - 3} + a_{n - 4}, n \geq 5$$ Applying the recurrence relation to the initial conditions yields \begin{align*} a_5 & = 29\\ a_6 & = 56\\ a_7 & = 108\\ a_8 & = 208 \end{align*} As a check, observe that of the $2^5 = 32$ bit strings of length $5$, the only ones that do not have fewer than four consecutive ones are $01111$, $11110$, and $11111$, so there are $32 - 3 = 29$ bit strings of length $5$ that have fewer than four consecutive ones, which agrees with our count.

Since there are $256$ bit strings of length $8$ and $208$ of these bit strings of length $8$ have fewer than four consecutive ones, the number of bit strings of length $8$ that have at least four consecutive ones is $256 - 208 = 48$.

N. F. Taussig
  • 79,492
  • Thanks for the answer. Is an alternate approach possible, that is based on finding without having a count of first few cases. I mean that the above approach seems like induction, as generates a formula based on few initial cases. I hence request a formula that is based on something like my approach. – jiten Oct 07 '18 at 00:58
  • 1
    This approach struck me as simpler than using the Inclusion-Exclusion Principle. To use the Inclusion-Exclusion Principle, I would define $A_i$, $1 \leq i \leq 5$, as the set of bit strings of length $8$ that include four consecutive ones beginning in the $i$th position. The problem is that these sets intersect when more than four consecutive ones occur, so we over count. Therefore, we must subtract those cases that occur more than once. Doing so requires care and considerable effort. – N. F. Taussig Oct 07 '18 at 01:12
  • Please elaborate your last comment, as am unable to understand how over-counting is possible. I mean that the case for $5$ consecutive ones is different from any case for $4$ consecutive ones, as on either side of $4$ consecutive ones there should be either nothing (as the block occurs in a corner) or a $0$; while $5$ consecutive ones is a different case. – jiten Oct 07 '18 at 03:20
  • 2
    Example if I understand definition of $A_i$ correctly. $11111000 \in A_1 \cap A_2$ – Siong Thye Goh Oct 07 '18 at 05:14
  • @SiongThyeGoh Am confused as to how the bit string $11111000$ can be in both categories. For me, the case $11111000$ cannot occur in for the case of $4$ consecutive bits' block. Please elaborate it, as I feel you mean by $A_1$ the case for $4$ consecutive $1$s; & by $A_2$ the case for $5$ consecutive $1$s. – jiten Oct 07 '18 at 06:00
  • 1
    $A_i$ has $1$ starting from position $i$, it is also possible to have $1$ at position $i-1$. $11111000$ has $5$ consecutive $1$. It is in $A_1$ since starting from position $1$, there are $5$ consecutive $1$'s. It is in $A_2$ since starting from position $2$, there are $4$ consecutive $1$'s. – Siong Thye Goh Oct 07 '18 at 06:05
  • @SiongThyeGoh I am confused even more. May be, am unable to grasp the crux of the author's comment at all, which talks of $A_i$. So, sorry cannot follow your comment. May be need big help to understand, so request chat. – jiten Oct 07 '18 at 06:50
  • Please accede to my request for chat. – jiten Oct 07 '18 at 07:11
  • hmm... I didn't receive any prompt for chat. – Siong Thye Goh Oct 07 '18 at 07:17
  • 1
    @SiongThyeGoh: there is a more clean and effective approach : re. to this related post which leads to the solution I presented above. – G Cab Oct 07 '18 at 19:34
  • @GCab Thanks for sharing. – Siong Thye Goh Oct 08 '18 at 00:53
  • @siongthyegoh: my pleasure, this the goal of this site! – G Cab Oct 08 '18 at 13:28
  • Sorry for so much delay in asking a small doubt. You stated under the initial conditions that the number of strings with one bit, given by $a_1$ are: $2.$ Next, you state that any string with $\lt 4$ bits can start in one of the four ways: $0, 10, 110, 1110.$ But, then $a_1$ comprises of two strings: $0,1.$ Do you mean that $a_2= a_1x, x\in{0,1};$ hence $10$ falls under $a_2?$ Similarly, $110\in a_3,$ and is formed by $a_2x,$ or $a_1xx.$ – jiten Jan 19 '23 at 07:10
  • Continuing from the last comment: And, $1\in a_1$ is part of 'initial condition, while $0, 10, 110, 1110$ are the prefix of any string. But, you didn't show that the two parts: (i) initial conditions, (ii) $4$ types of prefixes, are sufficient to generate all strings, with the 'number of $1$s' $\lt 4,$ for any string $a_n$ of length $n\geq 5.$ – jiten Jan 19 '23 at 07:48
  • 1
    You have to separate two concepts. The number $a_n$ is the number of admissible strings of length $n$. For the recurrence relation, I broke up the cases depending on the number of ones preceding the first zero. Since there can be at most three consecutive ones, the first zero must be in one of the first four positions. Any admissible string must either begin with $0$, $10$, $110$, or $1110$, depending on the position of the first zero. These cases are exhaustive. – N. F. Taussig Jan 19 '23 at 10:48
  • 1
    Since the string $0$ has length $1$, any admissible string of length $n$ that starts with $0$ must be followed by an admissible string of length $n - 1$, of which there are $a_{n - 1}$. Since the string $10$ has length $2$, any admissible string of length $n$ that starts with $10$ must be followed by an admissible string of length $n - 2$, of which there are $a_{n - 2}$, and so forth. I suggest that you count the number of admissible strings of length $4$ that begin with $0$, that begin with $10$, that begin with $110$, and that begin with $1110$ to see that the recurrence is correct. – N. F. Taussig Jan 19 '23 at 10:53
  • Thanks, but am still working on your kind response. Have an additional query: why $a_6= a_5+a_4+a_3+a_2= 2a_4+2a_3+2a_2+ a_1$ is unclear logically. Can only show by enumeration of the strings of length $6.$ Also, have got an enumeration based answer in your last response, for strings of length $4.$ Though this case is easier, as enumeration seems natural & only possible proof. The strings are: 1. begin with $0: 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111.$ 2. begin with $10: 1000, 1001, 1010, 1011.$ 3. begin with $110: 1100, 1101.$ 4. begin with $1110: 1110.$ I.e., a total of $8+4+2+1=15.$ – jiten Jan 19 '23 at 12:22
  • 1
    I am not sure why you are trying to express $a_6$ in terms of $a_4, a_3, a_2, a_1$. The point of the recurrence relation $a_n = a_{n - 1} + a_{n - 2} + a_{n - 3} + a_{n - 4}$ is that we can decompose any string of length $n \geq 4$ in which at most three consecutive ones appear into strings whose first zero is in the first position, second position, third position, or fourth position since such strings are mutually exclusive and exhaustive. Doing so for strings of length $6$ yields $a_6 = a_5 + a_4 + a_3 + a_2$. – N. F. Taussig Jan 19 '23 at 12:58
  • Thanks, but request some inputs on Inclusion-Exclusion based approach, that should remove overlap (as per your first comment). How to define the criteria to form different sets in that situation, that may not be mutually disjoint. The first answer is about that, but unable to understand the criteria of forming different sets – jiten Jan 26 '23 at 07:32
  • 1
    I have added two more solutions, one of which uses the Inclusion-Exclusion Principle. – N. F. Taussig Jan 26 '23 at 11:30
2

The numbers are small that a brute force way is possible (though not encouraged):

The bit strings can be of the following form:

  • $11110xyz$ : $2^3$ possibilities
  • $011110xy$ : $2^2$ possibilities
  • $x011110y$ : $2^2$ possibilities
  • $xy011110$ : $2^2$ possibilities
  • $xyz01111$ : $2^3$ possibilities
  • $111110xy$ : $2^2$ possibilities
  • $0111110x$ : $2^1$ possibilities
  • $x0111110$ : $2^1$ possibilities
  • $xy011111$ : $2^2$ possibilities
  • $1111110x$ : $2^1$ possibilities
  • $01111110$ : $2^0$ possibilities
  • $x0111111$ : $2^1$ possibilities
  • $11111110$ : $2^0$ possibilities
  • $01111111$ : $2^0$ possibilities
  • $11111111$ : $2^0$ possibilities

Hence there are a total of $$2(2^3)+5(2^2)+4(2)+4(1)=48 \text{ bit strings}$$

Edit:

If $A_i$ is the set of strings where positions $i$ to $i+3$ must take value $1$.

For this particular example, we note that $A_i \cap A_j \neq \emptyset$. Also, also for this particular question, the consecutive block of $1$ with lenght at least $4$ must be connected. if we assume $j>i$, in fact, we have $$|A_i \cap A_j | = 2^{8-((j+3)-i+1)}=2^{4-j+i}$$

Also, we have $A_i \cap A_j \cap A_k = A_{\max(i,j,k)} \cap A_{\min(i,j,k)}$ for this particular example.

With the help of Python, we can use inclusion exclusion principle to conclude that the cardinality is

\begin{align}\left| \bigcup_{i=1}^5 A_i\right|&=\sum_{i=1}^52^4- \sum_{i=1}^4 \sum_{j=i+1}^52^{4-j+i} + \sum_{i=1}^3 \sum_{j=i+1}^4\sum_{k=j+1}^52^{4-k+i}-\sum_{i=1}^2 \sum_{j=i+1}^3\sum_{k=j+1}^4\sum_{l=k+1}^52^{4-l+i}+1\\&=\sum_{i=1}^52^4 - \sum_{d=1}^4 (5-d)2^{4-d} + \sum_{d=2}^4(5-d) \binom{d-1}{1}2^{4-d}-\sum_{d=3}^4(5-d)\binom{d-1}2 2^{4-d}+1 \\&= 80 + \sum_{i=1}^4 (-1)^i\sum_{d=i}^4(5-d) \binom{d-1}{i-1}2^{4-d} \\&=80-49+23-7+1=48 \end{align}

Ѕᴀᴀᴅ
  • 35,400
Siong Thye Goh
  • 155,038
  • So, no generalized formula is possible (i.e., is not easy to use). Also, the 6th case should be $011110xy$ instead of $111110xy$. – jiten Oct 07 '18 at 05:52
  • 1
    The other answer has given you a generalized formula isn't it. $011110xy$ has been considered in the second case. – Siong Thye Goh Oct 07 '18 at 05:55
  • @jiten: actually there is a generalized formula, at least in terms of a finite summation. – G Cab Oct 07 '18 at 19:35
  • Please help with how the last link (in the last line of the answer) provided by @GCab. I am unable to find any link with this topic, except that it refers to triangular numbers. – jiten Oct 08 '18 at 10:21
  • Please be on chat for helping me understand the second code, as am not getting literature for that. I request you to join the last chatroom. – jiten Oct 08 '18 at 11:05
  • @jiten : the link is correct, the table (lower triangular matrix) shown is exactly what I called $M_b(n,r)$ – G Cab Oct 08 '18 at 13:23
  • I am too slow at learning the workings, have modified version at : https://py3.codeskulptor.org/#user302_xltvTRCSf4o9Vab_0.py to learn better. – jiten Oct 08 '18 at 15:12
  • Regarding your code (as stated in my Update2) request another approach (i.e., not based on in-built constructs like we did for the listing of sets' members before) for finding intersection of sets. I request you to be in the last chatroom. – jiten Oct 09 '18 at 18:21
  • Please come to chat room. – jiten Oct 10 '18 at 16:01
  • Request an unrelated, but quite imp. area for help. It concerns putting timer to measure (for comparison among different algos. used for a given problem) time taken in Python language. I came across this by a very simple (as compared to others) paper at : http://www.ijaerd.com/papers/finished_papers/ijaerd%2014-003.pdf, at pg. #4 (for impl. in C); but am unable to find out how the 'ticks' were given by author. Also, as earlier stated would like to know to put for slr. program in Python timers. – jiten Nov 08 '18 at 07:26
  • Also, in my chat (https://chat.stackexchange.com/rooms/84135/discussion-between-siong-thye-goh-and-jiten) with you I requested for explanation for the last line of @G Cab answer, as shown in the image at: https://i.sstatic.net/1OnZb.png – jiten Nov 08 '18 at 07:42
  • 1
    https://chat.stackexchange.com/rooms/85486/jiten not sure this is how chatroom works. avoid long comments. – Siong Thye Goh Nov 08 '18 at 07:47
  • Please check my post of today at : https://math.stackexchange.com/q/2994828/424260. It concerns analysis for overcounting by second approach as given in Q.24 of page #21 of the book: Combinatorics - A Guided Tour, by David Mazur. – jiten Nov 12 '18 at 04:27
  • 1
    i can't response much today ... i am sick... again.... maybe after i recover. – Siong Thye Goh Nov 12 '18 at 12:32
  • Please help with error in code running for extended version of your code above, for $n=8$ (rather than $n=4$) at https://py3.codeskulptor.org/#user302_KrQgseMSW2_3.py. – jiten Nov 27 '18 at 08:20
  • 1
    print out i and its length and examine what happens. – Siong Thye Goh Nov 27 '18 at 08:25
  • Am unable to grasp the significance of printing $i$ & its length, but would request discussion of the same at : https://chat.stackexchange.com/rooms/86261/8-bit-strings-having-4-contiguous. Sorry, am unable to include older comments in new chat room, please tell how to do. – jiten Nov 27 '18 at 08:31
  • The modified code for the last (new) chatroom at : https://chat.stackexchange.com/rooms/86261/8-bit-strings-having-4-contiguous) that concerns the issue of compilation error (for the modified program that checks for $4$ contiguous bits at : https://py3.codeskulptor.org/#user302_KrQgseMSW2_3.py); is at : https://py3.codeskulptor.org/#user302_w6he6jNaVn_0.py. – jiten Dec 14 '18 at 03:47
  • Please tell why my code at: http://www.codeskulptor.org/#user45_UuFKH9TCdZrv0vK_5.py gives error on introducing line#6, in new chat room at : https://chat.stackexchange.com/rooms/info/87190/stack-error-py3?tab=general. – jiten Dec 17 '18 at 16:22
  • Posted question at : https://stackoverflow.com/q/53854616/3693431.Please join chatroom. – jiten Dec 19 '18 at 16:02
  • Please join chatroom, for latest code : https://py3.codeskulptor.org/#user302_UKOWz5UMTD_0.py; & its further modification at line #16 (to point root to the current top of stack) at https://py3.codeskulptor.org/#user302_qPb3dtgj9D_0.py. – jiten Dec 26 '18 at 03:31
  • Thanks for updated fib(), but there is some issue concening l.v. $n$, that is not clear. The l.v. $n$ is wrongly specified in line #17 of your code (instead of id__) as at : https://py3.codeskulptor.org/#user303_qPPzihoo9W_4.py. And it leads to printing $n=5$ always, which is unclear. Second issue is that a modification of the code to add same issue with level number (as at: https://py3.codeskulptor.org/#user303_qPPzihoo9W_5.py) fails. Please join chatroom for this issue. – jiten Dec 30 '18 at 15:52
  • Please be in chatroom for the last issue, & its link with activation records used in recursion impl. of any p.l. – jiten Dec 31 '18 at 02:59
  • Please vet my post of today at : https://math.stackexchange.com/q/3088850/424260, & if possible give a code snippet that enables a better way to look at the problem. – jiten Jan 27 '19 at 02:06
  • I request some response to my earlier request. – jiten Jan 27 '19 at 03:33
  • Please detail some more on the Iverson's coefficients, as you used them to concisely express the nested sums? In particular, how that covers all terms. – jiten Jan 29 '23 at 03:28
2

The general solution is thoroughly explained in this related post, and is given by

Number of binary strings, with $s$ "$1$"'s and $m$ "$0$"'s in total, that have up to $r$ consecutive $1$s : $$ N_b (s,r,m + 1)\quad \left| {\;0 \leqslant \text{integers }s,m,r} \right.\quad = \sum\limits_{\left( {0\, \leqslant } \right)\,\,k\,\,\left( { \leqslant \,\frac{s}{r+1}\, \leqslant \,m + 1} \right)} { \left( { - 1} \right)^k \binom{m+1}{k} \binom {s + m - k\left( {r + 1} \right) }{s - k\left( {r + 1} \right) } } $$ and which are the coefficients of $x^s$ in $$ \left( {{{1 - x^{\,r + 1} } \over {1 - x}}} \right)^{\,m + 1} $$

Fixing the length to be $n$, summing over $m$ the above repartition into $n-m$ ones and $m$ zeros, we get

Number of binary strings of lenght $n$, that have up to $r$ consecutive $1$s : $$ M_b (n,r)\quad = \sum\limits_{\left( {0\, \le } \right)\,\,m\,\,\left( { \le \,n} \right)} { \sum\limits_{\left( {0\, \le } \right)\,\,k\,\,\left( { \le \,{{n-m} \over {r+1}}\, \le \,m + 1} \right)} { \left( { - 1} \right)^k \binom{m+1}{k} \binom{n - k\left( {r + 1} \right)}{ n - m - k\left( {r + 1} \right) } } } $$ which doesn't look to be further simplifiable.

In your case we have $$ M_b (8,r)\quad \left| {\;0 \le r \le 8} \right.\quad = 1,\;55,\;149,\;208,\;236,\;248,\;253,\;255,\;256 $$ thus $M_b (8,3)=208$, and the number of strings having $4$ or more consecutive ones is $2^8-208=48$

Note that those having exactly a maximum of $4$ consecutive ones are $M_b (8,4) -M_b (8,3)=28$

Finally, $M_b (n,r)$ is the OEIS sequence A126198 : "number of compositions of n into parts of size <= k".

G Cab
  • 36,129
  • Thanks, but feel that the last link is wrong, as it s about triangular numbers. Sorry, if wrong. – jiten Oct 08 '18 at 10:17
2

You requested an answer using the Inclusion-Exclusion Principle.

Let $A_i$ be the set of sequences of length $8$ with four consecutive ones beginning in the $i$th position. Such a sequence must begin in one of the first five positions, so the number of sequences with at least four consecutive ones is $|A_1 \cup A_2 \cup A_3 \cup A_4 \cup A_5|$.

By the Inclusion-Exclusion Principle, the number of such sequences is \begin{align*} |A_1 \cup A_2 \cup A_3 \cup A_4 \cup A_5| & = \sum_{i = 1}^{5} |A_i| - \sum_{1 \leq i < j \leq 5} |A_i \cap A_j| + \sum_{1 \leq i < j < k \leq 5} |A_i \cap A_j \cap A_k|\\ & \qquad - \sum_{1 \leq i < j < k < l \leq 5} |A_i \cap A_j \cap A_k \cap A_l| + |A_1 \cap A_2 \cap A_3 \cap A_4 \cap A_5| \end{align*}

$|A_1|$: If there is a $1$ in each of the first four positions, then there are two choices for each of the last four positions, so $|A_1| = 2^4$.

By symmetry, $|A_1| = |A_2| = |A_3| = |A_4| = |A_5|$.

$|A_1 \cap A_2|$: If there is a $1$ in the first four positions and the second four positions, then there is a $1$ in each of the first five positions. There are two choices for each of the last three positions, so $|A_1 \cap A_2| = 2^3$.

By symmetry, $|A_1 \cap A_2| = |A_2 \cap A_3| = |A_3 \cap A_4| = |A_4 \cap A_5|$.

$|A_1 \cap A_3|$: If there is a $1$ in the first four positions and the third four positions, then there is a $1$ in each of the first six positions. There are two choices for each of the last two positions, so $|A_1 \cap A_3| = 2^2$.

By symmetry, $|A_1 \cap A_3| = |A_2 \cap A_4| = |A_3 \cap A_5|$.

$|A_1 \cap A_4|$: If there is a $1$ in the first four positions and the fourth four positions, then there is a $1$ in each of the first seven positions. There are two choices for the eighth position, so $|A_1 \cap A_4| = 2$.

By symmetry, $|A_1 \cap A_4| = |A_2 \cap A_5|$.

$|A_1 \cap A_5|$: If there is a $1$ in the first four positions and the last four positions, then all eight positions are filled with $1$s, so $|A_1 \cap A_5| = 1$.

$|A_1 \cap A_2 \cap A_3|$: If there is a $1$ in the first four positions, second four positions, and third four positions, then the first six positions must be filled with $1$s. There are two choices for each of the last two positions, so $|A_1 \cap A_2 \cap A_3| = 2^2$.

By symmetry, $|A_1 \cap A_2 \cap A_3| = |A_2 \cap A_3 \cap A_4| = |A_3 \cap A_4 \cap A_5|$.

$|A_1 \cap A_2 \cap A_4|$: If there is a $1$ in the first four positions, second four positions, and fourth four positions, then the first seven positions must be filled with $1$s. There are two choices for the eighth position, so $|A_1 \cap A_2 \cap A_4| = 2$.

By symmetry, $|A_1 \cap A_2 \cap A_4| = |A_1 \cap A_3 \cap A_4| = |A_2 \cap A_3 \cap A_5| = |A_2 \cap A_4 \cap A_5|$.

$|A_1 \cap A_2 \cap A_5|$: If there is a $1$ in the first four positions, second four positions, and last four positions, then all eight positions must be filled with $1$s. Hence, $|A_1 \cap A_2 \cap A_5| = 1$.

By symmetry, $|A_1 \cap A_2 \cap A_5| = |A_1 \cap A_3 \cap A_5| = |A_1 \cap A_4 \cap A_5|$.

$|A_1 \cap A_2 \cap A_3 \cap A_4|$: If there is a $1$ in the first four positions, second four positions, third four positions, and fourth four positions, then the first seven positions must be filled with $1$s. There are two choices for the eighth position, so $|A_1 \cap A_2 \cap A_3 \cap A_4| = 2$.

By symmetry, $|A_1 \cap A_2 \cap A_3 \cap A_4| = |A_2 \cap A_3 \cap A_4 \cap A_5|$.

$|A_1 \cap A_2 \cap A_3 \cap A_5|$: If there is a $1$ in the first four positions, second four positions, third four positions, and last four positions, then all eight positions must be filled with $1$s. Hence, $|A_1 \cap A_2 \cap A_3 \cap A_5| = 1$.

By symmetry, $|A_1 \cap A_2 \cap A_3 \cap A_5| = |A_1 \cap A_2 \cap A_4 \cap A_5| = |A_1 \cap A_3 \cap A_4 \cap A_5|$.

$|A_1 \cap A_2 \cap A_3 \cap A_4 \cap A_5|$: If there is a $1$ in the first four positions, second four positions, third four positions, fourth four positions, and last four positions, then all eight positions must be filled with $1$s. Hence, $|A_1 \cap A_2 \cap A_3 \cap A_5| = 1$.

Therefore, by the Inclusion-Exclusion Principle, the number of binary strings of length $8$ with at least four consecutive $1$s is \begin{align*} |A_1 \cup A_2 \cup A_3 \cup A_4 \cup A_5| & = 5 \cdot 2^4 - 4 \cdot 2^3 - 3 \cdot 2^2 - 2 \cdot 2 - 1\\ & \qquad + 3 \cdot 2^2 + 4 \cdot 2 + 3 \cdot 1 - 2 \cdot 2 - 3 \cdot 1 + 1\\ & = 5 \cdot 2^4 - 4 \cdot 2^3\\ & = 48 \end{align*} Notice that unlike the solution I wrote using a recurrence relation for the number of sequences of length $n$ that do not contain at least four consecutive ones, this solution does not generalize to all $n$.

N. F. Taussig
  • 79,492
  • Does your last remark imply, also that for no. of bits $(n) >8,$ say for $n=9,$ can have multiple blocks of four bits, say $111101111?$ Does such as a possibility make generalization impossible? Couldn't find more cases that don't generalize to all $n,$ as think bounds of start locations of block of $4$ consecutive $1s$ can be changed suitably as: $i=1 $ to $n/2+1,$ & similarly for other indices. No. of indices of blocks of four $1s$ can be changed to $n/2+1.$ Though, that is still not a generalisation as inspite of these modifications, a general formula is not derivable. – jiten Jan 27 '23 at 02:17
  • 1
    Yes, it is possible for a bit string of length greater than $8$ to have disjoint blocks of four consecutive ones. The Inclusion-Exclusion Principle can still be applied if the length of the bit string exceeds $8$. However, you have to do an increasing number of cases, which makes using the Inclusion-Exclusion Principle less practical than using a recurrence relation. – N. F. Taussig Jan 27 '23 at 02:28
  • The computation makes it clear that for intersections of $2,3,4$ sets. Say, for the case of two sets at a time, there are $\binom 5 2$ possibilities, and different number of free bits are there for different categories in that. But, still confused how using Iverson's coefficients, the selected answer does the same so concisely. – jiten Jan 28 '23 at 12:36
  • I am not sure what you are asking. However, if you are asking about somebody else's answer, I suggest you ask that person. – N. F. Taussig Jan 28 '23 at 13:21
  • @SiongThyeGoh has stopped visiting mse for a long time. – jiten Feb 01 '23 at 04:04
  • I tried to apply the IE principle many times, with the basic $5$ sets formed. But, get confused why each set must have exactly $4$ consecutive $1$s. Have to again & again revert back to the example of finding students passing all subjects, by considering sets of students passing each subject. There need know intersection values among the different sets, unlike here - as can calculate the same. If some visual means could be found, then would be better. – jiten Feb 01 '23 at 05:45
  • 1
    Keep in mind that $A_1$ contains the binary strings that have ones in each of the first four positions. Those strings contain elements which may have ones in any of the last four positions such as $11110000$ or $11110101$ or $11111001$ or $11111111$. Saying that there is a one in each of the first four positions does not preclude having a one in the other positions. Since we are free to fill each of the last four positions with either a $0$ or $1$, $|A_1| = 2^4$. – N. F. Taussig Feb 01 '23 at 09:50
  • 1
    Now consider $A_1 \cap A_2$. We know that there must be a $1$ in each of the first four positions and each of the second four positions. Since those positions overlap, there must be a $1$ in each of the first five positions. We can place a $0$ or a $1$ in each of the last three positions, which is why $|A_1 \cap A_2| = 2^3$. – N. F. Taussig Feb 01 '23 at 09:53
  • You seem to take that for $A_2,$ the first bit can be a $0/1,$ hence taking $\mid A_1\cap A_2\cap A_3 =$ $ \mid A_2\cap A_3\cap A_4\mid = 2^2$ $= \mid A_3\cap A_4\cap A_5\mid.$ This confuses, as am taking for $A_2,$ the bit #$1$ to have no choice, except to be $0.$ Though, for $A_3,$ the bit #$1$ can be $0/1,$ but the bit #$2$ should have no choice, except to be $0.$ As per me, $\mid A_2\cap A_3\cap A_4\mid = 2$ $= \mid A_3\cap A_4\cap A_5\mid.$ Though, $\mid A_1\cap A_2\cap A_3\mid = 2^2.$ – jiten Apr 27 '23 at 12:53
  • Notice that I defined $A_i$ to be a binary string with four consecutive ones beginning in the $i$th position. In order to understand my answer, you have to use that definition, which permits the other entries in the string to be either $0$ or $1$. – N. F. Taussig Apr 27 '23 at 12:56
  • But, that definition then differs from that of the selected answer. And, in fact that uses a different definition, than mine, as does not allow the trailing first bit to have a choice. So, there are 3 definitions: as I allow all the trailing bits to have a choice. – jiten Apr 27 '23 at 12:57
  • 1
    That is correct. – N. F. Taussig Apr 27 '23 at 13:00
  • The selected answer has the simplest definition, among the three. – jiten Apr 27 '23 at 13:03
  • But, even the selected answer considers your definition, for the purpose of deriving a formula, in the 'Edit'. Else, it is not possible to have, in that answer: $\mid A_2\cap A_4\mid$ $ = 2^{4-4+2}= 2^{2},$ as by the definition there, it should be $=2^0=1$ instead, as the single bit at each end must be $=0.$ – jiten Apr 27 '23 at 14:59
  • 1
    I am not sure what you are saying. Under my definition, neither the first bit nor the last bit needs to be equal to zero in $A_2 \cap A_4$ since the strings in $A_2 \cap A_4$ are required to have ones in positions two through seven. Also, if you compare Siong Thye Goh's answer in the edit with mine, you will notice that his $|A_1 \cup A_2 \cup A_3 \cup A_4 \cup A_5| = 80 - 49 + 23 - 7 + 1$ matches my $|A_1 \cup A_2 \cup A_3 \cup A_4 \cup A_5| = 5 \cdot 2^4 - 4 \cdot 2^3 - 3 \cdot 2^2 - 2 \cdot 2 - 1 + 3 \cdot 2^2 + 4 \cdot 2 + 3 \cdot 1 - 2 \cdot 2 - 3 \cdot 1 + 1$. – N. F. Taussig Apr 27 '23 at 17:39
1

Let $A_i$, $1 \leq i \leq 5$, be the number of binary sequences in which the first string containing at least four consecutive $1$s begins in the $i$th position.

$|A_1|$: The first four positions must be filled with $1$s. There are two choices for each of the last four positions, so there are $2^4$ such strings.

$|A_2|$: Since the string of at least four consecutive $1$s begins in the second position, the first position must be a $0$. Positions $2$ through $5$ must be filled with $1$s. There are two choices for each of the last three positions, so there are $2^3$ such strings.

$|A_3|$: Since the string of at least four consecutive $1$s begins in the third position, the second position must be a $0$. Positions $3$ through $6$ must be filled with $1$s. There are two choices for each of the other three positions, so there are $2^3$ such strings.

$|A_4|$: Since the string of at least four consecutive $1$s begins in the fourth position, the third position must be a $0$. Positions $4$ through $7$ must be filled with $1$s. There are two choices for each of the other three positions, so there are $2^3$ such strings.

$|A_5|$: Since the string of at least four consecutive $1$s begins in the fifth position, the fourth position must be a $0$. Positions $5$ through $9$ must be filled with $1$s. There are two choices for each of the other three positions, so there are $2^3$ such strings.

Since these five cases are mutually exclusive and exhaustive, the number of strings of length $8$ with at least four consecutive $1$s is $$|A_1| + |A_2| + |A_3| + |A_4| + |A_5| = 2^4 + 4 \cdot 2^3 = 48$$ Note that this method does not generalize to binary strings of length greater than $8$ since such strings can have two (or more) mutually disjoint strings of at least four consecutive ones.

N. F. Taussig
  • 79,492
0

Let's correct your original attempt. If there are at least four consecutive ones in a bit string of length eight, then the bit string must contain a substring with exactly four consecutive ones, exactly five consecutive ones, exactly six consecutive ones, exactly seven consecutive ones, or eight consecutive ones.

Exactly four consecutive ones: If the four consecutive ones are in the first four positions, the fifth position must be a zero, and there are two choices for each of the last three positions. Hence, there are $2^3$ such strings.

If the four consecutive ones are in the last four positions, the fourth position must be a zero, and there are two choices for each of the first three positions. Hence, there are $2^3$ such strings.

If the string of exactly four consecutive ones begins in the second, third, or fourth positions, then there must a zero immediately before and immediately after the string of four consecutive ones. That leaves two choices for each of the remaining two positions. Hence, there are $3 \cdot 2^2$ such strings.

Since these three cases are mutually exclusive and exhaustive, there are $$2 \cdot 2^3 + 3 \cdot 2^2 = 28$$ bit strings containing exactly four consecutive ones.

Exactly five consecutive ones: If the five consecutive ones are in the first five positions, the sixth position must be a zero, and there are two choices for each of the last two positions. Hence, there are $2^2$ such strings.

By symmetry, there are $2^2$ strings in which the five consecutive ones are in the last five positions.

If the string of five consecutive ones begins in the second or third positions, there must be a zero immediately before and immediately after the string. That leaves two choices for the remaining position. Hence, there are $2 \cdot 2$ such strings.

Since these cases are mutually exclusive and exhaustive, there are $$2 \cdot 2^2 + 2 \cdot 2 = 12$$ bit strings containing exactly five consecutive ones.

Exactly six consecutive ones: If the string of six consecutive ones begins in the first position, the seventh position must be a zero, and there are two choices for the last position.

By symmetry, there are two bit strings in which the string of six consecutive ones is in the last six positions.

If the six consecutive ones begins in the second position, both the first and last positions must be filled with zeros.

Since these cases are mutually exclusive and exhaustive, there are $$2 \cdot 2 + 1 = 5$$ bit strings with exactly six consecutive ones.

Exactly seven consecutive ones: Either the first or last position must be equal to zero. Hence, there are $2$ bit strings with exactly seven consecutive ones.

Eight consecutive ones: All the positions must be filled with ones, so there is one such string.

Total: Since the above cases are mutually exclusive and exhaustive, there are $28 + 12 + 5 + 2 + 1 = 48$ bit strings with at least four consecutive ones.

N. F. Taussig
  • 79,492