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.
n
or the firstn+1
numbers? \$\endgroup\$