[Question]
  [
The *binary-square-diagonal-sequence* is constructed as follows:
1. Take the sequence of positive natural numbers: 
```
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, ...
```
2. Convert each number to binary: 
```
1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, 10000, 10001, ...
```
3. Concatenate them: 
```
11011100101110111100010011010101111001101111011111000010001 ...
```
4. Starting with `n=1`, generate squares with increasing side-length `n` which are filled left-to-right, top-to-bottom with the elements of the above sequence:
```
1
```
```
1 0
1 1
```
```
1 0 0 
1 0 1
1 1 0
```
```
1 1 1 1
0 0 0 1
0 0 1 1 
0 1 0 1
```
```
0 1 1 1 1
0 0 1 1 0
1 1 1 1 0
1 1 1 1 1
0 0 0 0 1
```
```
...
```
5. Take the diagonal (top left to bottom right) of each square: 
```
1, 11, 100, 1011, 00111, ...
```
6. Convert to decimal (ignoring leading zeros):
```
1, 3, 4, 11, 7, ...
```
# Task
Write a program or function which outputs the sequence in one of the following ways:
* Return or print the sequence infinitely.
* Given input `i`, return or print the first `i` elements of the sequence.
* Given input `i`, return or print the `i`th element of the sequence (either 0 or 1 indexed).
Please state in your answer which output format you choose. 
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), the shortest answer in each language wins. 
# Test cases
Here are the first 50 elements of the sequence: 
`1,3,4,11,7,29,56,141,343,853,321,3558,8176,3401,21845,17129,55518,134717,151988,998642,1478099,391518,7798320,8530050,21809025,61485963,66846232,54326455,221064493,256373253,547755170,4294967295,1875876391,2618012644,24710258456,6922045286,132952028155,217801183183,476428761596,51990767390,687373028085,1216614609441,7677215985062,15384530216172,22714614479340,15976997237789,0,256145539974868,532024704777005,601357273478135`
  
[Answer]
## [Husk](https://github.com/barbuz/Husk), ~~15~~ 14 bytes
```
zȯḋm←CtNCİ□ṁḋN
```
[Try it online!](https://tio.run/##yygtzv7/v@rE@oc7unMftU1wLvFzPrLh0bSFD3c2AoX8/v8HAA "Husk – Try It Online")
Continually prints the results as an infinite list.
### Explanation
I wonder whether there's a better way to get every **n**th element from a list than splitting the list into chunks of length **n** and retrieving the head of each chunk.
```
      tN          Get a list of all natural numbers except 1. (A)
             N    Get a list of all natural numbers.
           ṁḋ     Convert each to its binary representation and join them 
                  all into a single list.
         İ□       Get a list of squares of all natural numbers.
        C         Cut the list of bits into chunks of corresponding sizes. (B)
zȯ                Zip (A) and (B) together with the following function.
     C            Split the bit list (from B) into chunks of the given length
                  (from A).
   m←             Get the head of each chunk. This is the diagonal of the
                  bit list arranged as a square.
  ḋ               Interpret the resulting bits as binary digits and return
                  the result.
```
To be clear, we extract the diagonal of an **n x n** square by splitting its linear form into chunks of length **n + 1** and retrieving the first element of each chunk:
```
[[1 , 0 , 1 , 0
  0],[1 , 0 , 1
  1 , 0],[1 , 0
  0 , 1 , 0],[1]]
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~137~~ 85 bytes
```
lambda n:int(''.join(bin(x+1)[2:]for x in range(n**3))[n*~-n*(2*n-1)/6:][:n*n:n+1],2)
```
[Try it online!](https://tio.run/##Rc2xDsIgFEbhWZ/ibnApqNDEgURfBBloFKXRv03ToS6@OtbJ4YxfzvieHwNczadLfaZXd00EXzBLIXb9UCC7taWxHJyPeZhooQKaEu43CaVa5gD1MVDSKRjL@6OPwUPBo7FRO64/VP7Iantgv92M03qhokmYs9CUZeHafgE "Python 2 – Try It Online")
[Answer]
# [Python 3](https://docs.python.org/3/), ~~96~~ 94 bytes
```
lambda i:int(''.join(bin(x+1)[2:]for x in range(i**3))[sum(x*x for x in range(i))::i+1][:i],2)
```
[Try it online!](https://tio.run/##XYxBCoMwEAC/sjezUQpRelnwJTGHSFvdohuJCunrU/VS6GFOM8zy2cYgTZa2y5Of@4cHJpZNFcXtHVhUf5BKg7Ym9woRErBA9DI8FWvdINp1n1XSCf4tIhGXxlliV9WYl3h@rRzmavnXmupu0GH@Ag "Python 3 – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~19~~ ~~17~~ 16 bytes
```
°LbJsLn£θs>ô€нJC
```
`°` is replaced by `3m` in the links as `°` tends to get very slow.
[Try it online!](https://tio.run/##MzBNTDJM/f/fONcnyavYJ@/Q4nM7iu0Ob3nUtObCXi/n///NAA "05AB1E – Try It Online")
or as a [Test Suite](https://tio.run/##ASoA1f8wNWFiMWX/MjBMdnl5ef8zbUxiSnNMbsKjzrhzPsO04oKs0L1KQ//Lhv8)
**Explanation**
```
°L                 # push the range [1 ... 10^input]
  bJ               # convert each to binary and join to string
    sLn            # push the range [1 ... input]^2
       £θ          # split the binary string into pieces of these sizes and take the last
         s>ô       # split this string into chunks of size (input+1)
            €н     # get the first digit in each chunk
              JC   # join to string and convert to int
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 15 bytes
This takes a somewhat different approach to [Martin's](https://codegolf.stackexchange.com/users/8478/martin-ender) [answer](https://codegolf.stackexchange.com/a/145428/71256)
```
moḋz!NCNCṘNNṁḋN
```
[Try it online!](https://tio.run/##yygtzv7/Pzf/4Y7uKkU/Zz/nhztn@Pk93NkIFPD7/x8A "Husk – Try It Online")
### Explanation:
```
              N   List of all natural numbers
            ṁḋ    Convert each to it's binary representation and flatten
         ṘNN      Repeat the list of natural numbers according the natural numbers:
                  [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5...]
        C         Cut the list of bits into lists of lengths corresponding to the above
      CN          Cut that list into lists of lengths corresponding to the natural numbers
moḋz!N            For each in the list, get the diagonals and convert from binary.
m                   For each list in the list
   z!N              Zip it with natural numbers, indexing.
 oḋ                 Convert to binary
```
### In action
`ṁḋN` : `[1,1,0,1,1,1,0,0,1,0,1,1,1,0,1,1,1,1,0,0,0,1,0,0,1,1,0,1,0,1...]`
`ṘNN` : `[1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,7,8,8...]`
`C` : `[[1],[1,0],[1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1,1],[0,0,0,1]...]`
`CN` : `[[[1]],[[1,0],[1,1]],[[1,0,0],[1,0,1],[1,1,0]]...]`
`m z!N` : `[[1],[1,1],[1,0,0],[1,0,1,1],[0,0,1,1,1],[0,1,1,1,0,1]...]`
`oḋ` : `[1,3,4,11,7,29,56,141,343,853,321,3558,8176,3401,21845...]`
[Answer]
# [Java (OpenJDK 8)](http://openjdk.java.net/), ~~215~~ ~~212~~ ~~206~~ ~~202~~ 197 bytes
```
i->{String b="",t;int s=0,x=++i,j;for(;--x>0;s+=x*x);while(b.length()<s)b+=i.toString(++x,2);for(j=1,s=0;j<i;System.out.println(i.valueOf(t,2)),s+=j*j++)for(t="",x=s;x<s+j*j;x+=j+1)t+=b.charAt(x);}
```
[Try it online!](https://tio.run/##fZA/b8MgEMVn@1OgTFBslFTqhIlUtUuHqoPHqgN2iIOLwTFHShTls7vkT9fe@O69u6dfLw@ydKOy/eZ71sPoJkB90lgAbdg22Ba0s@zFWR8GNfE8H0NjdItaI71H71JbdMozDxKS@Ger3iyoTk1r1Ggrp2O9D3JSr1p2zkpTq31QtlVIoFmX61MNk7YdasRiUQDXFpAXyyIKSnXR862bMC/LuF5yT0V8iIT/7LRRuGFG2Q52mFSeNFRoBu52ClMai0dyjfZiVaRzvK80r48e1MBcADYmHxiLNTtIE9THFkNKkCK96B96SsklC5dGUXgeK0@TzGPa0hUBKhrW7uT0DDjVOc8JS3bncidxcHqDhkQH3yp9fiFJEil0n@w/MEy2rRoBPy0Jvyayc36efwE "Java (OpenJDK 8) – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), 91 bytes
```
i=n=1;s=''
while 1:
 s+=bin(i)[2:];i+=1
 if s[n*n:]:print int(s[:n*n:n+1],2);s=s[n*n:];n+=1
```
[Try it online!](https://tio.run/##LYw7CoAwEAX7nGI7P7FJyg17kphGUFyQpxhBPH2MYPGaYeYdz7Xu8AUyFhWIC1maxtyrbjM5NpStTIpWu@g5BbXiDOlCOaIHJz5OxUV1bY78IViXBt/Vm18JqE0pLw "Python 2 – Try It Online")
prints the sequence infinitely
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 16 bytes
```
RBFṁ
R²SÇṫ²C$m‘Ḅ
```
[Try it online!](https://tio.run/##ASwA0/9qZWxsef//UkJG4bmBClLCslPDh@G5q8KyQyRt4oCY4biE/8OH4oKs//8xMA "Jelly – Try It Online")
## Explanation
```
RBFṁ  Helper link. Input: integer k
R     Range, [1, 2, ..., k]
 B    Convert each to a list of its binary digits
  F   Flatten
   ṁ  Mold to length k
R²SÇṫ²C$m‘Ḅ  Main link. Input: integer n
R            Range, [1, 2, ..., n]
 ²           Square each
  S          Sum
   Ç         Call helper link on the sum of the first n squares
       $     Monadic chain
     ²         Square n
      C        Complement, 1-n^2
    ṫ        Tail, take the last n^2 elements
        m    Modular indexing, take each
         ‘   (n+1)th element
          Ḅ  Convert from list of binary digits to decimal
```
[Answer]
# Mathematica, 96 bytes
**Outputs** the `i`th element of the sequence (1-indexed) 
```
Diagonal@Partition[TakeList[Flatten@IntegerDigits[Range[#^3],2],Range@#^2][[#]],#]~FromDigits~2&
```
  
[Try it online!](https://tio.run/##JcqxCsIwEADQXwkEnDJIxFHIUAqCQyluxxUOucbD5gLpzf31CDo@eIXszYVMXtRXd3N9EMpVaUsTNROTqvCkDz9kNxg3MmNNdzXO3AbJYjvMpJnBLxcMEcNPyS8RATxi8HiMrZb/PeKpT03UXHIrXM/Yvw "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 18 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
Completely different approach compared to [Erik's solution](https://codegolf.stackexchange.com/a/145430/59487).
```
Ḷ²S‘ɓ*3B€Fṫ
Çm‘ḣµḄ
```
[Try it online!](https://tio.run/##AS8A0P9qZWxsef//4bi2wrJT4oCYyZMqM0LigqxG4bmrCsOHbeKAmOG4o8K14biE////Ng "Jelly – Try It Online")
## How it works
```
Ḷ²S‘ɓ*3B€Fṫ  - Helper link (monadic).
Ḷ            - Lowered range, generates [0, N).
 ²           - Vectorized square (square each).
  S          - Sum.
   ‘         - Increment, to account for the 1-indexing of Jelly.
     ɓ       - Starts a separate dyadic chain.
     *3      - The input to the power of 3.
       B€    - Convert each to binary.
         F   - Flatten.
          ṫ  - Tail. Return x[y - 1:] (1-indexed).
Çm‘ḣµḄ  - Main link (monadic).
Ç       - Last link as a monad.
 m‘     - Modular input + 1. Get each "input + 1"th element of the list.
   ḣ    - Head. Return the above with elements at index higher than the input cropped.
    µḄ  - Convert from binary to integer.
```
Saved 1 byte thanks to [Jonathan Allan](https://codegolf.stackexchange.com/users/53748/jonathan-allan)!
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 22 bytes
```
²Rµ²Ṭ€Ẏ0;œṗBẎ$³ịs³ŒDḢḄ
```
[Try it online!](https://tio.run/##ATgAx/9qZWxsef//wrJSwrXCsuG5rOKCrOG6jjA7xZPhuZdC4bqOJMKz4buLc8KzxZJE4bii4biE////Ng "Jelly – Try It Online")
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), ~~27~~ 20 bytes
```
i<%hQ>s.BS^Q3s^R2QQ2
```
**[Verify the first few test cases.](https://pyth.herokuapp.com/?code=i%3C%25hQ%3Es.BS%5EQ3s%5ER2QQ2&test_suite=1&test_suite_input=1%0A2%0A3%0A4%0A5%0A6%0A7%0A8&debug=0)**
Gets the **I**th term of the sequence, 1 indexed.
## How it works?
```
i<%hQ>s.BS^Q3s^R2QQ2   - Full program. Q represents the input.
         S^Q3          - Generate the (inclusive) range [1, Q ^ 3].
       .B              - Convert each to binary.
      s                - Join into a single string.
     >                 - Trim all the elements at indexes smaller than:
               ^R2Q      - The elements of the range [0, Q) squared.
              s          - And summed.
  %hQ                  - Get each Q + 1 element of the list above.
 <                     - Trim all the elements at indexes higher than:
                   Q   - The input.
i                   2  - Convert from binary to integer.
```
[Answer]
# [Perl 5](https://www.perl.org/), 92 + 1 (`-p`) = 93 bytes
```
$s.=sprintf'%b',++$"for 1..$_**3;map{say oct"0b".(substr$s,0,$_*$_,'')=~s/.\K.{$_}//gr}1..$_
```
[Try it online!](https://tio.run/##HctBCsIwEADAr5SwJdqmmxTJSfoC8QdCaMRKQZOQjYdS6tNdxftMuuWHZQbCgVKeQ5lk7aVqWxBTzFWPCK5pDsfnmFYalypeizBe4I5enkoGUkb9BDgl5X54k8bLCVdwm9b3vP07szWfmMocA3F3tmh6w134Ag "Perl 5 – Try It Online")
]