# The Binary Square Diagonal Sequence

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, ...

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 ith element of the sequence (either 0 or 1 indexed).

This is , 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 

## Husk, 15 14 bytes

zȯḋm←CtNCİ□ṁḋN


Try it online!

Continually prints the results as an infinite list.

### Explanation

I wonder whether there's a better way to get every nth 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]]


# Python 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!

# Python 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!

# 05AB1E, 1917 16 bytes

°LbJsLn£θs>ô€нJC


° is replaced by 3m in the links as ° tends to get very slow.

Try it online! or as a Test Suite

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

• Can't you replace 3m with n? – Erik the Outgolfer Oct 16 '17 at 12:02
• @EriktheOutgolfer: Yeah I can, thanks! I was pretty sure that didn't work, but that may have been due to kinks in an earlier solution. Same byte count as ° but much faster :P – Emigna Oct 16 '17 at 12:06
• The numbers from 1 to input^2 are not sufficient. 1 to input^3 like in the python answers seems to be enough. – ovs Oct 16 '17 at 12:11
• @ovs: Ah yes, that's why I didn't use it previously. I only checked the first couple of items this time. I'll revert to the previous solution (fortunately at the same byte count) – Emigna Oct 16 '17 at 12:12

# Husk, 15 bytes

This takes a somewhat different approach to Martin's answer

moḋz!NCNCṘNNṁḋN


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...]

# Java (OpenJDK 8), 215212206202 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!

# Python 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!

prints the sequence infinitely

# Jelly, 16 bytes

RBFṁ
R²SÇṫ²C$m‘Ḅ  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  # Mathematica, 96 bytes Outputs the ith element of the sequence (1-indexed) Diagonal@Partition[TakeList[Flatten@IntegerDigits[Range[#^3],2],Range@#^2][[#]],#]~FromDigits~2&  # Perl 5, 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! # Jelly, 18 bytes Completely different approach compared to Erik's solution. Ḷ²S‘ɓ*3B€Fṫ Çm‘ḣµḄ  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! • Save one using a dyadic chain to remove the ³: Ḷ²S‘ɓ*3B€Fṫ – Jonathan Allan Oct 16 '17 at 21:22 • @JonathanAllan Of course, thanks! I should really learn that trick – Mr. Xcoder Oct 17 '17 at 4:22 # Jelly, 22 bytes ²Rµ²Ṭ€Ẏ0;œṗBẎ$³ịs³ŒDḢḄ


Try it online!

# Pyth,  27  20 bytes

i<%hQ>s.BS^Q3s^R2QQ2


Verify the first few test cases.

Gets the Ith 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.