17
Consider the infinite matrix:
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
0 0 2 3 0 0 2 3 0 0 2 3 0 0 2 3
0 0 0 4 5 6 0 0 0 4 5 6 0 0 0 4 ...
0 0 0 0 7 8 9 10 0 0 0 0 7 8 9 10
0 0 0 0 0 11 12 13 14 15 0 0 0 0 0 11
...
Each new row of the matrix is constructed by starting with z
zeros, where z
is the length of positive digits we're using in that row. The positive digits are constructed by starting with 1
and incrementing and adding an additional digit each time you iterate rows. That pattern is repeated infinitely to the right. So, for example, the first row starts 0, 1, 0, 1...
while the second row starts 0,0, 2,3, 0,0, 2,3...
. Following the pattern, the third row starts 0,0,0, 4,5,6, 0,0,0, 4,5,6...
.
Given two integers as input, n
and x
, output the first (top-most) x
numbers of the n
th column of the above matrix. (You can choose 0- or 1-indexing for the columns, just specify which in your submission.)
For example, for input n = 0
(0-indexed), the column is entirely 0
s, so the output would just be x
0
s.
For input n = 15
and x = 6
, the output would be [1, 3, 4, 10, 11, 0]
.
For input n = 29
and x = 15
, the output would be [1, 0, 6, 8, 15, 0, 0, 34, 39, 0, 0, 0, 0, 0, 120]
.
For input n = 99
and x = 25
, the output would be [1, 3, 4, 0, 15, 0, 0, 0, 37, 55, 56, 0, 87, 93, 0, 0, 151, 163, 176, 0, 0, 0, 0, 0, 325]
.
I/O and Rules
- The input and output can be given by any convenient method.
- The input and output can be assumed to fit in your language's native number type.
- Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
- Standard loopholes are forbidden.
- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
It errors for
– JayCe – 2018-05-16T15:53:33.103n=1
since the result of sapply in no longer a matrix. this fix is costly I wonder if there is a golfier one?Oh, yeah, you're right. Well, luckily there is one! – Giuseppe – 2018-05-16T15:58:17.607
The for loop, yeah! And you golfed 4 bytes in the process :) – JayCe – 2018-05-16T16:01:48.517
@JayCe yeah my original thought was to index the outermost
rep
with ann
inside thesapply
, which saved a byte, but then I remembered thatfor
loops are shorter thansapply
since I wouldn't have to define a function. – Giuseppe – 2018-05-16T16:04:52.343