[Question]
  [
Let's consider the sequence of the binary representation of positive integers (without any leading zero):
```
1 2  3  4   5   6   7   8    9    10   11   12   ...
1 10 11 100 101 110 111 1000 1001 1010 1011 1100 ...
```
If we join them together, we get:
```
1101110010111011110001001101010111100 ...
```
If we now look for the patterns `/1+0+/`, we can split it as follows:
```
110 11100 10 1110 1111000 100 110 10 10 111100 ...
```
We define \$s\_n\$ as the length of the \$n\$-th pattern built that way. Your task is to generate this sequence.
The first few terms are:
```
3, 5, 2, 4, 7, 3, 3, 2, 2, 6, 3, 5, 9, 4, 4, 2, 3, 4, 3, 2, 2, 3, 3, 2, 8, 4, 4, 2, 3, 7, 4, 6, 11, 5, 5, ...
```
Related OEIS sequence: [A056062](https://oeis.org/A056062), which includes the binary representation of \$0\$ in the initial string and counts \$0\$'s and \$1\$'s separately.
## Rules
You may either:
* take \$n\$ as input and return the \$n\$-th term, 1-indexed
* take \$n\$ as input and return the \$n\$-th term, 0-indexed
* take \$n\$ as input and return the \$n\$ first terms
* take no input and print the sequence forever
This is a [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") challenge.
## Some more examples
The following terms are 1-indexed.
```
s(81)    = 13
s(100)   = 3
s(101)   = 2
s(200)   = 5
s(1000)  = 5
s(1025)  = 19
s(53249) = 29
```
  
[Answer]
# [Husk](https://github.com/barbuz/Husk), 7 bytes
```
mLġ≤ṁḋN
```
[Try it online!](https://tio.run/##yygtzv7/P9fnyMJHnUse7mx8uKPb7/9/AA "Husk – Try It Online")
Takes no input and prints ALL the numbers!
### Explanation
```
mLġ≤ṁḋN
      N        The list of all positive integers [1,2,3...]
    ṁḋ         Convert each to binary and concatenate the resulting digits
  ġ≤           Split them in groups where each digit is less than or equal to the previous one (basically cuts wherever there is a 0 followed by a 1)
mL             Compute the length of each group
```
[Answer]
# [Python](https://docs.python.org/3.8/), ~~77~~ 67 bytes
```
lambda n:len(''.join(f'{i:b}'for i in range(9*n)).split('01')[n])+2
```
[Try it online!](https://tio.run/##VVDLToUwEN3zFbNrK@MNbUEeSV36Be64LLhatAYLKbBQwrcjd4w3MZnHyZmTk5kZv@b3wetiDHtnznvffl5eW/BVbz1n7PQxOM87trrqsrFuCODAeQitf7O8vPNCnKaxdzNniWSi9o2I1R7M@hwWWy3svKhcaobw1PbTH5G@sC2yptYIGYJCSBFyBE2hKB4IH9OSpimRmsBNc9MX/zU54cNBSrLIsIlocbTX1b/dyHs3zfz3BonXQ62IpRBxXUiUSXKkREWdisow0yotG7RxLTVqVHhQKEtUZSOqCCbTcSciGIPzM71rg/tHWKcN1lBbY6ZmY2L/AQ "Python 3.8 (pre-release) – Try It Online")
Returns the \$n^\text{th}\$ term, 1-indexed.
[Answer]
# [MATL](https://github.com/lmendo/MATL), 15 bytes
```
E:"@B]v&Y'2esG)
```
This takes `n` as input and outputs the `n`-th term, 1-indexed.
[Try it online!](https://tio.run/##y00syfn/39VKycEptkwtUt0otdhd8/9/QwMjUwA "MATL – Try It Online")
### Explanation
A binary pattern of the specified form ends at least as often as every even number. So for input `n`, considering the numbers `1`, `2`, ..., `2*n` guarantees that at least `n` patterns are obtained.
```
E      % Implicit input: n. Push 2*n
:"     % For each k in [| 2 ... 2*n]
  @    %   Push k
  B    %   Binary expansion. Gives a row vector containing 1's and 0's
]      % End
v      % Concatenate everything into a column vector
&Y'    % Lengths of run-length encoding. Runs contain 1's and 0's alternately
2e     % Reshape as a two-column matrix, in column-major order
s      % Sum of each column. This gives the lenghts of the desired patterns
G)     % Take the n-th entry. Implicit display
```
[Answer]
# [Haskell](https://www.haskell.org/), 80 bytes
```
([1..]>>=f)#0
f 0=[]
f x=f(div x 2)++[mod x 2]
(0:1:x)#l=l+1:x#1
(a:x)#l=x#(l+1)
```
[Try it online!](https://tio.run/##JYhLCsMgEED3nmLALBRp0C4Dk4tYF5LEKjGpNKF4@kwtXb1P9Me65EwRHySs6Xs3jhgk1yyARusaKgYxpw9UuEul7Paaf@qY0IMZquQZs2rCDRP@35WLtiRtPu2AUN5pP6GD068LGA2Rrilk/zzoNpXyBQ "Haskell – Try It Online")
Inspired by [Leo's Husk answer](https://codegolf.stackexchange.com/a/201403/64121), calculates an infinite list.
[Answer]
# [Octave](https://www.gnu.org/software/octave/), 62 bytes
```
@(n)diff(regexp([arrayfun(@dec2bin,1:4*n,'un',0){:}],'1+'))(n)
```
[Try it online!](https://tio.run/##y08uSSxL/Z9mq6en999BI08zJTMtTaMoNT21okAjOrGoKLEyrTRPwyElNdkoKTNPx9DKRCtPR700T13HQLPaqjZWR91QW11TE6jzf0lqcYmCrUK0haGCoYEBEBsqGIFpMGFkGmvNBTcwTUcBpFzzPwA "Octave – Try It Online")
### Explanation
```
@(n)                                                           % function with input n
                                   1:4*n                       % range [1, 2, ... 4*n]
                 arrayfun(@dec2bin,     ,'un',0)               % convert each to binary string
                [                               {:}]           % concat into one string
         regexp(                                    ,'1+')     % starting indices of runs of 1's
    diff(                                                 )    % consecutive differences
                                                           (n) % take n-th entry
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~12~~ 11 bytes
```
ḤB€FI»0kƲẈḣ
```
[Try it online!](https://tio.run/##ASEA3v9qZWxsef//4bikQuKCrEZJwrswa8ay4bqI4bij////MjA "Jelly – Try It Online")
A monadic link taking an integer \$n\$ and returning the first \$n\$ terms of the series.
Change from `×9` to `Ḥ` inspired by @JonathanAllan’s answer. Thanks!
[Answer]
# [Ruby](https://www.ruby-lang.org/), 48 bytes
```
->n{("%b%b"*n%[*1..n*2]).scan(/1+0+/)[n-1].size}
```
[Try it online!](https://tio.run/##VYzRCoJAEEV/ZREEdce1cTXroX5kWUJLaR9aJIu21G/fbB6E4DIc5h7u/dm8fXfw6dGOURA2YRMkNlQJCmGTXMdiONc2ypBveBYrm6IWg/m0s1cSWAksB1YAq4BJSk7ZEi/tntqCnpJgdVZ/9@9UxMsCIk2UWtzqXrzM43oy9tK6cXJgpp4pwxG639Xg9Oy/ "Ruby – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 9 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
∞bSγ2ôεSg
```
~~Untested, since TIO isn't working.. >.> But it should work (unless one of those builtins used isn't lazy).  
I'll try to finally install 05AB1E locally later today to verify if it indeed works.~~  
EDIT: Installed 05AB1E locally, and apparently it didn't work due to the `J`oin on the infinite list. So here an alternative 9-byter that does actually work.
Outputs the infinite sequence.
[Try it online.](https://tio.run/##yy9OTMpM/f//Uce8pOBzm40Obzm3NTj9/38A)
**Explanation:**
```
∞          # Push an infinite list of positive integers: [1,2,3,4,5,6,...]
 b         # Convert each to a binary string
           #  → ["1","10","11","100","101","110",...]
  S        # Convert it to a flattened list of digits
           #  → [1,1,0,1,1,1,0,0,1,0,1,1,1,0,...]
   γ       # Split them into parts of consecutive equal digits
           #  → [[1,1],[0],[1,1,1],[0,0],[1],[0],[1,1,1],[0],...]
    2ô     # Split all that into parts of size 2
           #  → [[[1,1],[0]],[[1,1,1],[0,0]],[[1],[0]],[[1,1,1],[0]],...]
      ε    # Map over each pair
       S   #  Convert it to a flattened list of digits again
           #   → [[1,1,0],[1,1,1,0,0],[1,0],[1,1,1,0],...]
        g  #  Pop and push its length
           #   → [3,5,2,4,...]
           # (after which the mapped infinite list is output implicitly as result)
```
[Answer]
# [Perl 5](https://www.perl.org/) -n, 73 bytes
```
$_=join'',map{sprintf"%b",$_}1..($n=$_)*2;say y///c for(/1+0+/g)[0..$n-1]
```
[Try it online!](https://tio.run/##BcFdCoJAEADgq4hMqOnOT@BTeIROELFYZGzU7LLri0RXb/q@dM@v0Qz89IxBm2Z4z@lTUg66LvXuWg/gv4LYgk7gu/3hWOat2ojoVi0xtyQ99/TozowI6uRiJsy/mNYQtZg7jcjC5vQP "Perl 5 – Try It Online")
Takes input `n` via stdin, prints the first `n` numbers in the sequence.
[Answer]
# [K (ngn/k)](https://bitbucket.org/ngn/k), 26 bytes
```
{x##'(&0>':t)_t:,/2\'!2*x}
```
[Try it online!](https://tio.run/##y9bNS8/7/z/NqrpCWVldQ83ATt2qRDO@xEpH3yhGXdFIq6L2f5qCscF/AA "K (ngn/k) – Try It Online")
Returns the first `n` items.
# [J](http://jsoftware.com/), 35 bytes
```
{[:((1,2</\])#;.1])@;[:#:&.>[:i.3&*
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/q6OtNDQMdYxs9GNiNZWt9QxjNR2so62UrdT07KKtMvWM1bT@a3IpcKUmZ@QraFinaSoZaBhqZ@oZGWj@BwA "J – Try It Online")
Returns the `n`th item
[Answer]
# [Haskell](https://www.haskell.org/), ~~135~~ 128 bytes
* Saved seven bytes thanks to [ovs](https://codegolf.stackexchange.com/users/64121/ovs).
```
g$b=<<[1..]
b 0=[];b n=b(div n 2)++[mod n 2]
l(1:r)1=1+l r 1;l(0:r)0=1+l r 0;l(0:r)1=1+l r 0;l(1:r)0=0
g a=l a 1:g(drop(l a 1)a)
```
[Try it online!](https://tio.run/##Tcy7CoNAFATQfr9ighYrouyapPCxhYRgE4JNKmOxohhxfaCSJuTbjRELbzVnBu5LjnWh1PyxNNzCe/QIoysucQzN@pJRPOdSz0QQJNy2U5KBiST1M7Qio3n1RgvHMM2k6fJ/TImi3BsMLripMID7irLFbDPbzHfm685ICSkUJLhX0nzoerrCkMbcyKqFQN4RLNcPVTvBxiTrAq4LHeOuX4TDAfR8dE4urOXB/AM "Haskell – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 11 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
ḤB€FŒgẈ+2/ḣ
```
A monadic Link accepting an integer, `n`, which yields a list of the first `n` values.
**[Try it online!](https://tio.run/##ASAA3/9qZWxsef//4bikQuKCrEbFkmfhuogrMi/huKP///8xMA "Jelly – Try It Online")**
### How?
```
ḤB€FŒgẈ+2/ḣ - Link: integer, n
Ḥ           - double (n)
  €         - for each v in (implicit range = [1..2n]):
 B          -   (v) to binary
   F        - flatten
    Œg      - group runs
      Ẉ     - get lengths
        2/  - 2-wise reduce by:
       +    -   addition
          ḣ - head to index (n)
```
[Answer]
# bash + GNU utilities, ~~76~~ ~~58~~ 57 bytes
```
seq -f 2o%.fn $[2*$1]|dc|sed -E "s/(1*0*){$1}.*/\1Zp/"|dc
```
[Try it online!](https://tio.run/##FYo7DsIwEAV7TvG0MhJYss0GpYSOS/ApIF4rNGvAdEnObkwz0ozmcS9jTfkDxVMxsff7flkBMTfIMGY4BRnF4Qhqqcj3r1SLvOESurz2SWEunTV8m@MwF4lwJ1AJG7Y7u50ML96GK59fgdpQY1apPw "Bash – Try It Online")
*Thanks to user41805 for suggestions that ended up shaving 18 bytes off! And for 1 more byte now too.*
Takes \$n\$ as an argument, and prints the \$n^\text{th}\$ entry in the sequence (with 1-based indexing).
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~34~~ 22 bytes
```
≔…⌕Ａ⭆⊗⊕θ⍘ι²01⊕θηＩ⁻⊟η⊟η
```
[Try it online!](https://tio.run/##VY3BCsIwEETvfkXoaQMRrNeeaqTgoVDwC2IamsC6qUkq@PVxpSfnMsPMg7HeJBsN1trnHBYC/bHotI8rDIHmHhHuJQVaRrPCNW4PdDPcyCb3dFQ4v6RU4mKy2zEISpx/VXNqG7Z/VAkvu8PEYAFtcoEx0JZh4jfP4@6srta2Ht/4BQ "Charcoal – Try It Online") Link is to verbose version of code. Based on @LuisMendo's observation that the numbers up to `2n` provide sufficient digits, although I search for `01` so I actually need `0` through `2n+1`. Explanation:
```
⭆⊗⊕θ⍘ι²
```
Convert all the numbers from `0` to `2n+1` to base 2 and concatenate them.
```
≔…⌕Ａ...01⊕θη
```
Find the positions of the substrings `01` but truncated after the `n`th entry.
```
Ｉ⁻⊟η⊟η
```
Output the difference between the last two positions.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~15~~ 14 bytes
```
×3ŻBFœṣØ.ḊẈ+2ḣ
```
[Try it online!](https://tio.run/##ASYA2f9qZWxsef//w5czxbtCRsWT4bmjw5gu4biK4bqIKzLhuKP///8xMA "Jelly – Try It Online") Thanks to @JonathanAllan and @NickKennedy for helping me out, in chat, to finish this solution. I came up with [`×3RBFœṣØ.Ẉ+2ḣ`](https://tio.run/##ASIA3f9qZWxsef//w5czUkJGxZPhuaPDmC7huogrMuG4o////zEw) but that fails for `n = 1`!
**How it works**:
```
×3ŻBFœṣØ.ḊẈ+2ḣ    Monadic link: takes `n` as input and returns the first `n` terms
×3                Multiply input by three and
  Ż                create the list [0, 1, ..., 3n].
   B              Get the binary representation of each number and
    F              flatten to get [0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, ...]
                  Now we find the /1+0+/ patterns by looking at occurrences of [0, 1],
                   i.e. when one pattern ends and the next begins:
     œṣ           Split the [0, 1, 1, 0, 1, 1, 1, 0 ...] list at occurrences of
       Ø.         [0, 1], so [0, 1, 1, 0, 1, 1, 1, 0 ...] -> [[], [1], [1, 1, ...], ...]
         Ḋ         and drop the first element of the resulting list (the empty one).
          Ẉ       Finally we get the length of each sublist,
           +2      add 2 (to compensate for the lost 1 and 0),
             ḣ     and take the first `n` elements of that.
```
[Answer]
# [Red](http://www.red-lang.org), ~~122~~ 111 bytes
```
func[n][b: copy""repeat i 2 * n[append b find enbase/base
to#{}i 2"1"]parse b[n copy i[any"1"any"0"]]length? i]
```
[Try it online!](https://tio.run/##PYyxDsIwDET3foVlNiREga0D/AOr5SFpHIiEXCtNhwrx7SFlYHkn3Z1ellDvEoi7ONS46EjK5AcYJ1sRs5i4AgnOsAclZyYawENMLUS9m@W4oSvT7v1pNzwhm8uzgCf9SSCR07X1G3tkfok@yvMGietff@nJctJCCfBwRYht5foF "Red – Try It Online")
[Answer]
# [PHP](https://php.net/), 108 bytes
```
for($a=[$p=$i=1];;$p=$c,$o++){if(!$a)$a=str_split(decbin(++$i));if($p<$c=array_shift($a)){echo$o,',';$o=0;}}
```
[Try it online!](https://tio.run/##FYxBCsMgEADfUliIix6as1n6kFKCtREXSndRLyHk7dZchjkMo1n78tDBJMVAoCcoAdP88v6y6ECsxYOTuUHAEdRW1qpfbuazxTf/jLXAiH4UoAtECqWEfa2ZUxtDxGOLWUDc5CYPQnd/nr3/AQ "PHP – Try It Online")
Will print the sequence indefinitely.
[Answer]
# [Gaia](https://github.com/splcurran/Gaia), 13 bytes
```
ḣ┅b¦_ėt(2/Σ¦E
```
[Try it online!](https://tio.run/##AR4A4f9nYWlh///huKPilIViwqZfxJd0KDIvzqPCpkX//zM "Gaia – Try It Online")
Port of Luis' [MATL answer](https://codegolf.stackexchange.com/a/201400/67312).
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 11 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
Outputs the `n`th 1-indexed term.
```
g°U²ô¤¬ò<)l
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=Z7BVsvSkrPI8KWw&input=OA)
```
g°U²ô¤¬ò<)l     :Implicit input of integer U
g               :Index into
 °U             :  Increment U
   ²            :  Square it
    ô           :  Range [0,result]
     ¤          :  To binary strings
      ¬         :  Join
       ò<       :  Partition after characters that are less than the next
         )      :End indexing
          l     :Length
```
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~124~~ \$\cdots\$ ~~109~~ 104 bytes
Saved ~~2~~ ~~3~~ ~~4~~ ~~8~~ ~~9~~ 14 bytes thanks to [Arnauld](https://codegolf.stackexchange.com/users/58563/arnauld)!!! 
```
c;t;b;i;f(n){for(i=c=0,t=1;++i;){for(b=0;i>>++b;);for(;b--;++c)if(t^i>>b&1&&(t^=1)?c*=!--n:0)return c;}}
```
[Try it online!](https://tio.run/##dY3NCsIwEITvPsUqWBLTQOvPQdfUFxHBrEYCGqWNGJC@ujH17@achvlmd0geiGIk9KjRomGO3825ZlaRKnKvShTC4jvTqkBbVUJo5NgFqKVMnLg1zG8S0lmZZcmqkq9opPpSukXB672/1g4I2zaettYxDvceJL2GnIeQZsJSTWYYhPjCTh1sQIFhgeMvvdQpN2ww3IGsYLhbu0EOIYfm02l7f1uzyXg6z9O/l@HpoI0PMsftoYny9gQ "C (gcc) – Try It Online")
Goes through positive integers \$i\$ catching transitions from \$0\$ to \$1\$ as it rolls through the non-leading-zero bits of the \$i\$'s.
Returns the \$n^\text{th}\$ term, 1-indexed.
[Answer]
# [W](https://github.com/A-ee/w) [`x`](https://codegolf.meta.stackexchange.com/questions/14337/command-line-flags-on-front-ends/14339#14339), 7 [bytes](https://github.com/A-ee/w/wiki/Code-Page)
REALLY slow. The array is 1-indexed and it outputs all upto the input. (Glad that I tie with Husk BTW. Special bonus: it doesn't involve infinite lists!)
```
♫│x╤►U╟
```
Uncompressed:
```
^k2BLHkr
```
# Explanation
```
^        % 10 ^ input. Make sure that enough items are calculated.
 k       % Find the length range of that.
  2B     % Convert every item to binary.
         % Since at least 1 item >= the base, this vectorizes.
         % Automatic flatten before grouping
    LH   % Grouping: Is the previous item >= current item?
      kr % Reduce by length
Flag:x  % Output all items upto the input, including input-indexed item. 1-indexed.
```
# [W](https://github.com/A-ee/w) [`x`](https://codegolf.meta.stackexchange.com/questions/14337/command-line-flags-on-front-ends/14339#14339), 8 [bytes](https://github.com/A-ee/w/wiki/Code-Page)
You can try this without having to wait for a long time.
```
☺│╪å∟↕c╟
```
Uncompressed:
```
3*k2BLHkr
```
# Explanation
```
3*         % Input times 3, idea copied from RGS's answer.
  k        % Provide a length-range
   2B      % Convert all to binary
     LH    % Group by >=
           % Automatic flattening before grouping
       kr  % Reduce by length
Flag:x      % Output all less than the input index. INCLUDING the input index item.
```
```
[Answer]
# [APL (Dyalog Extended)](https://github.com/abrudz/dyalog-apl-extended), 17 bytes
```
{⍵⊃≢¨⊆⍨1+∊⊤¨⍳+⍨⍵}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/v/pR79ZHXc2POhcdWvGoq@1R7wpD7UcdXY@6lgD5vZu1gQJAFbX/0x61TXjU2/eob6qnP1D9ofXGj9omAnnBQc5AMsTDM/i/uYKCrUKagimXoTGYYWHIZQwRMjQw4DKCMQ25TCFMI6CoKVyBAZehJZRtZMplBGGbGhuZWAIA "APL (Dyalog Extended) – Try It Online")
Gives nth term, 1-indexed.
### How it works
```
{⍵⊃≢¨⊆⍨1+∊⊤¨⍳+⍨⍵}
{               }  ⍝ ⍵←n
             +⍨⍵   ⍝ Double of n
            ⍳      ⍝ 1 .. 2n, inclusive
         ∊⊤¨  ⍝ Convert each to binary and flatten
       1+     ⍝ Add 1
     ⊆⍨       ⍝ Partition self into non-increasing segments
              ⍝ (Without 1+, zero items are dropped)
   ≢¨  ⍝ Lengths of each segment
 ⍵⊃    ⍝ Take nth item
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 67 [bitsv2](https://github.com/Vyxal/Vyncode/blob/main/README.md), 8.375 bytes
```
Þ∞bfĠ2ẇvf@
```
[Try it Online!](https://vyxal.pythonanywhere.com/?v=2&c=1#WyI9IiwiIiwiw57iiJ5iZsSgMuG6h3ZmQCIsIiIsIiJd)
Bitstring:
```
0010011101110110001111101100111001011111001011101000111011000011110
```
[Answer]
# [Factor](https://factorcode.org/), 92 bytes
```
: f ( n -- n ) dup 3 * [0,b] [ >bin ] map concat "01" " " replace " " split nth length 2 + ;
```
[Try it online!](https://tio.run/##LY49D4JAEER7f8XEyi8MSgeJhY2xoTFWhOI4F7yI67kshb8eTzCbzLzqzdbG6kuG6@Wcn1I8SJhaPI3ex9h6Ix3JxGK4oQ4dvXti@yPfOlXHDbyQ6seLY0U2O@cpjo6NfCJvVIOzG1LUWIARRSGWuPUeCVYo4k1VosChcowy7HjYF1ujmMe7OX4n5FtjaeRxERy@a4mbUHuskQ1JjGI3ieq/ZTt8AQ "Factor – Try It Online")
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), 29 bytes
```
{⍵+.=+\2</∊,(2∘⊥⍣¯1)¨⍳3+⍵}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v//Ud/UR20Tqh/1btXWs9WOMbLRf9TRpaNh9KhjxqOupY96Fx9ab6h5aMWj3s3G2kBFtUD1//8DAA "APL (Dyalog Classic) – Try It Online")
*Will post explanation soon!*
[Answer]
# [Zpr'(h](https://www.github.com/jfrech/Zpr-h/), 369 bytes
```
s |> \
```
```
(g (foldr (op-> ++) () (map b |N)))
(e ())|>o
(e (S ()))|>z
(e (S (S .n)))|>(e n)
(h ())|>()
(h (S ()))|>()
(h (S (S .n)))|>(S (h n))
(b ())|>()
(b (S .n))|>((b (h (S n))) ++ (' (e n) ()))
(l (' z (' o .r)))|>1
(l (' z (' z .r)))|>(S (l (' z r)))
(l (' o (' o .r)))|>(S (l (' o r)))
(l (' o (' z .r)))|>(S (l (' z r)))
(g .a)|>(' (l a) (g (drop (l a) a)))
<|prelude.zpr
```
```
main |> (take 8 s)
```
# Execution
```
Zpr-h-master/stdlib$ ../Zprh --de-peano above.zpr
(' 3 (' 5 (' 2 (' 4 (' 7 (' 3 (' 3 (' 2 0))))))))
```
# Explanation
```
; build the sequence by splitting the bits of all natural numbers |N
sequence |> (generate (foldr (op-> ++) () (map bits |N)))
; compute if a natural number is even (parity shifted by one)
(even ())         |> one
(even (S ()))     |> zero
(even (S (S .n))) |> (even n)
; halve a natural number, rounding down
(halve ())         |> ()
(halve (S ()))     |> ()
(halve (S (S .n))) |> (S (halve n))
; compute a natural number's binary representation
(bits ())     |> ()
(bits (S .n)) |> ((bits (halve (S n))) ++ (' (even n) ()))
; compute the length of the pattern sought after at the bit stream's beginning
(len (' zero (' one .rest)))  |> 1
(len (' zero (' zero .rest))) |> (S (len (' zero rest)))
(len (' one (' one .rest)))   |> (S (len (' one rest)))
(len (' one (' zero .rest)))  |> (S (len (' zero rest)))
(generate .all-bits) |> (' (len all-bits) \
                           (generate (drop (len all-bits) all-bits)))
; include from the standard library
<| prelude.zpr
; output the first eight sequence members
main |> (take 8 sequence)
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 70 bytes
```
Tr[1^Join@@Partition[Split[Join@@IntegerDigits[Range[2#],2]],2][[#]]]&
```
[Try it online!](https://tio.run/##JYixCsIwEEB/RSg4BdqkFnQQMrjoVNTtOOGQEA/aq6S3id9@ah3ee/BG0kcaSflOlvd2LeBvp4klxp6KsvIkcHkOrPC/R9GUUzlwZp3hTJIThApdwB8AFSKurS8suqpjruNr651vmi/ehaWLQue6Nmx2b7MP "Wolfram Language (Mathematica) – Try It Online")
]