The binary-square-diagonal-sequence is constructed as follows:
- Take the sequence of positive natural numbers:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, ...
Convert each number to binary:
1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, 10000, 10001, ...
Concatenate them:
11011100101110111100010011010101111001101111011111000010001 ...
Starting with
n=1
, generate squares with increasing side-lengthn
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
...
Take the diagonal (top left to bottom right) of each square:
1, 11, 100, 1011, 00111, ...
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 firsti
elements of the sequence. - Given input
i
, return or print thei
th element of the sequence (either 0 or 1 indexed).
Please state in your answer which output format you choose.
This is 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
13 Answers 13
Husk, (削除) 15 (削除ここまで) 14 bytes
zȯḋm←CtNCİ□しろいしかくṁḋN
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)
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)
05AB1E, (削除) 19 (削除ここまで) (削除) 17 (削除ここまで) 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
-
\$\begingroup\$ Can't you replace
3m
withn
? \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年10月16日 12:02:54 +00:00Commented Oct 16, 2017 at 12:02 -
\$\begingroup\$ @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 \$\endgroup\$Emigna– Emigna2017年10月16日 12:06:11 +00:00Commented Oct 16, 2017 at 12:06 -
\$\begingroup\$ The numbers from 1 to input^2 are not sufficient. 1 to input^3 like in the python answers seems to be enough. \$\endgroup\$ovs– ovs2017年10月16日 12:11:18 +00:00Commented Oct 16, 2017 at 12:11
-
\$\begingroup\$ @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) \$\endgroup\$Emigna– Emigna2017年10月16日 12:12:58 +00:00Commented Oct 16, 2017 at 12:12
Husk, 15 bytes
This takes a somewhat different approach to Martin's answer
moḋz!NCNCṘNNṁḋN
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), (削除) 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);}
-
\$\begingroup\$
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);}
(197 bytes) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年10月17日 11:20:20 +00:00Commented Oct 17, 2017 at 11:20
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
prints the sequence infinitely
Jelly, 16 bytes
RBFṁ
R2SÇṫ2C$m‘Ḅ
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
R2SÇṫ2C$m‘Ḅ Main link. Input: integer n
R Range, [1, 2, ..., n]
2 Square each
S Sum
Ç Call helper link on the sum of the first n squares
$ Monadic chain
2 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 i
th element of the sequence (1-indexed)
Diagonal@Partition[TakeList[Flatten@IntegerDigits[Range[#^3],2],Range@#^2][[#]],#]~FromDigits~2&
Jelly, 18 bytes
Completely different approach compared to Erik's solution.
Ḷ2S‘ɓ*3B€Fṫ
Çm‘ḣμḄ
How it works
Ḷ2S‘ɓ*3B€Fṫ - Helper link (monadic). Ḷ - Lowered range, generates [0, N). 2 - 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!
-
\$\begingroup\$ Save one using a dyadic chain to remove the
³
:Ḷ²S‘ɓ*3B€Fṫ
\$\endgroup\$Jonathan Allan– Jonathan Allan2017年10月16日 21:22:33 +00:00Commented Oct 16, 2017 at 21:22 -
\$\begingroup\$ @JonathanAllan Of course, thanks! I should really learn that trick \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年10月17日 04:22:25 +00:00Commented Oct 17, 2017 at 4:22
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.
Perl 5, 92 + 1 (-p
) = 93 bytes
$s.=sprintf'%b',++$"for 1..$_**3;map{say oct"0b".(substr$s,0,$_*$_,'')=~s/.\K.{$_}//gr}1..$_
Explore related questions
See similar questions with these tags.