15
Given an integer n
, output the first n
sloping binary numbers, either 0- or 1-indexed. They are called this because of how they are generated:
Write numbers in binary under each other (right-justified):
........0
........1
.......10
.......11
......100
......101
......110
......111
.....1000
.........
Then, you need to take each diagonal from bottom-left to top-right, such that each final digit is the final digit of a diagonal. Here's the fourth diagonal (zero-indexed) marked with x
's, which is 100
:
........0
........1
.......10
.......11
......10x
......1x1
......x10
......111
.....1000
.........
The upward-sloping diagonals in order are:
0
11
110
101
100
1111
1010
.......
Then, convert to decimal, giving 0, 3, 6, 5, 4, 15, 10, ...
This is code-golf, so the shortest code in bytes wins.
12I don't think this specification is very clear. I had to do a good deal of external reading before I could understand what was being asked here. – Post Rock Garf Hunter – 2016-12-22T23:13:27.597
1Here's a visualization, if it helps. Read the "ovals" top to bottom, and within the oval from bottom left to top right. Those give you the binary numbers you need to convert to decimal. – Pavel – 2016-12-22T23:35:47.253
What do you mean, "either 0- or 1-indexed"? Do you mean that one may output either the first
n
or the firstn+1
numbers? – smls – 2016-12-22T23:49:18.070@Flp.Tkc yes, thanks for pointing that out. I edited the question and tagged it as such. (not OP) – Rɪᴋᴇʀ – 2016-12-23T00:20:22.103
Hopefully I made it clearer. – mbomb007 – 2016-12-23T01:19:08.590
4I think this might have allowed more interesting answers if you just had to return the n'th value. – xnor – 2016-12-23T03:27:33.683
@xnor I thought people might just use the formula on OEIS either way. I didn't know which would be better. – mbomb007 – 2016-12-23T04:04:31.590
The specification makes sense to me, but if you're going to output the first N numbers sloped up like this, shouldn't you be generating the first N + binlen(N) or so numbers? – Patrick Roberts – 2016-12-23T18:09:56.593
1@PatrickRoberts I never put a limit on how many to generate. I simply said "write numbers in binary...". You generate as many as you need to. – mbomb007 – 2016-12-23T18:11:02.873