Progression of Matrix Columns

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 nth 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 0s, so the output would just be x 0s.

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 so all usual golfing rules apply, and the shortest code (in bytes) wins.

JavaScript (ES6), 45 bytes

Takes input in currying syntax (n)(x).

n=>g=x=>x?[...g(x-1),n/x&1&&n%x+x*~-x/2+1]:[]


Try it online!

How?

We use a direct formula to get the value of the cell at column n (0-indexed) and row x (1-indexed):

n / x & 1 &&     // is this cell zero or non-zero?
n % x +          // column modulo row --> increment for a non-zero value at this position
x * ~-x / 2 + 1  // minimum value of non-zero values for this row:
// ∑(i=1...x-1)(i) + 1 = x(x - 1) / 2 + 1


R, 80 76 bytes

Thanks to @JayCe for pointing out a bug!

function(n,x)for(a in 1:x)print(rep(c(rep(0,a),((y=sum(1:a))-a+1):y),,n)[n])


Try it online!

Uses 1-based indexing for n. Very likely a golfier algorithm exists but rep is the enabler for the naive solution.

• It errors for n=1 since the result of sapply in no longer a matrix. this fix is costly I wonder if there is a golfier one? May 16, 2018 at 15:53
• Oh, yeah, you're right. Well, luckily there is one! May 16, 2018 at 15:58
• The for loop, yeah! And you golfed 4 bytes in the process :) May 16, 2018 at 16:01
• @JayCe yeah my original thought was to index the outermost rep with an n inside the sapply, which saved a byte, but then I remembered that for loops are shorter than sapply since I wouldn't have to define a function. May 16, 2018 at 16:04

Python 2, 69 bytes

n,x=input()
a=i=1
exec"print(([0]*i+range(a,a+i))*n)[n];a+=i;i+=1;"*x


Try it online!

APL (Dyalog Classic), 2724 23 bytes

-1 thanks to @FrownyFrog

⊢∘⍳(<×(+\⊣)--){+\⍵/2}|⊣


Try it online!

• 1 shorter {+\⍵/2}|⊣ May 16, 2018 at 18:36

MATL, 25 18 bytes

x:"@:t~ys:b@-)h1G)


Try it online!

Thanks to Luis Mendo for golfing out 6 bytes!

This is essentially a MATL port of my R answer.

x		 % implicit input, read n and delete
:		 % implicit input, read x and push [1..x]
"		 % for loop with i = 1..x
@:		 % push [1..i]
t		 % duplicate
~		 % logical negate, turn to array of zeros
% stack: [[1..i], [0 .. (i times)]]
y		 % duplicate from below
% stack: [[1..i], [0 .. (i times)], [1..i]]
s:	 % sum and range
% stack: [[1..i], [0 .. (i times)], [1..(i * (i + 1)/2)]]
b	 % bubble up
% stack: [[0 .. (i times)], [1..(i * (i + 1)/2)], [1..i]]
@-	 % push i and subtract. This will be used as a modular index to get the last i elements
% stack: [[0 .. (i times)], [1..(i * (i + 1)/2)], [1-i..0]]
)	 % index into array modularly to get the last i elements
% stack: [[0 .. (i times)], [(i-1)*i/2 + 1, .. (i * (i + 1)/2)]]
h	 % horizontally concatenate the array
1G) % push n and index modularly, leaving the result on the stack
% implicit end of for loop. The stack now contains the appropriate elements in order
% implicit end. Print stack contents


K (ngn/k), 33 31 bytes

{(a>i)*(+\i)-i-a:(2*1+i:!y)!'x}


Try it online!

Husk, 14 bytes

↑!Tzo¢+MRN0CNN


The argument n (first) is 1-indexed, try it online!

Alternatively we could use ↑!TṠzo¢+†K0CNN for the same number of bytes.

Explanation

↑!Tz(¢+)MRN0CNN -- example inputs n=4, x=3
C N -- cut the natural numbers: [1,2,3,4,…]
N  -- | using the natural numbers
-- : [[1],[2,3],[4,5,6],[7,8,9,10],…]
M N     -- map over the naturals
R 0    -- | replicate 0 that many times
-- : [[0],[0,0],[0,0,0],[0,0,0,0],…]
z(  )        -- zip these two lists
+         -- | concatenate
¢          -- | cycle
-- : [[0,1,0,1,…],[0,0,2,3,0,0,2,3,…],…]
T             -- transpose: [[0,0,0,0,…],[1,0,0,0,…],[0,1,0,0,…],[1,3,4,0,…],…]
!              -- index into that list using n: [1,3,4,0,…]
↑               -- take x: [1,3,4]


Husk, 21 19 bytes

↑mȯ!⁰¢§+R0§…ȯ→Σ←ΣN


Takes arguments as n (1-indexed), then x.
Saved 2 bytes thanks to BMO, but still not as short as BMO's answer.
My first attempt at using Husk.
Try it online!

u!v=take v[cycle((0<$l)++l)!!u|n<-[1..],l<-[take n$drop(sum[1..n-1])[1..]]]


Try it online!

Python 2, 55 bytes

lambda n,x:[n/y%2*(n%y+y*~-y/2+1)for y in range(1,x+1)]


Try it online!

Developed independently; but I note that this ends up being a port of Arnauld's Javascript answer.

Perl 5-n, 52 bytes

/ /;say+(((0)x$_,($.+=--$_)..$.+$_)x$)[$]for 1..$'


Try it online!

Jelly, 11 bytes

ṖS+R¬;$)⁹ịⱮ  Try it online! -1 thanks to Jonathan Allan. Argument 1: x Argument 2: n + 1 • 0ṁ;Ɗ -> ¬;$ saves a byte. May 16, 2018 at 21:34
• @JonathanAllan And I suspect that's not yet enough. At least I removed the humiliating ’R...(that is, at least for me) The weird thing is that during the last few days I've been thinking of the Thue-Morse sequence (which, in Jelly, contains ;¬$). May 17, 2018 at 11:24 05AB1E, 25 bytes LO©LsL£¹£¹LÅ0s)øε˜®Ì∍}ø¹è  Try it online! 05AB1E goes with matrices like toothpaste and orange juice, but not a bad byte-count considered how bad my implementation is. Even my code is laughing at me "LO©L". Charcoal, 19 bytes ＩＥ…·¹Ｎ§⁺Ｅι⁰ＥιＬ⊞Ｏυωη  Try it online! Link is to verbose version of code. Explanation:  ¹ Literal 1 Ｎ First input (x) as a number …· Inclusive range Ｅ Map (value i, 0-indexed counter k) ι Current value Ｅ Map over implicit range ⁰ Literal 0 (i.e. create array of i zeros) ι Current value Ｅ Map over implicit range ω Arbitrary variable υ Predefined array ⊞Ｏ Push Ｌ Length ⁺ Concatenate arrays § η Index by second input (n) Ｉ Cast to string and implicitly print on separate lines  The snippet ＥιＬ⊞Ｏυω generates the next i integers by the expedient of pushing a dummy value to an array each pass through the loop and taking the length of the resulting array. Java 8, 6563 60 bytes n->x->{for(;x>0;)System.out.println(n/x%2*(n%x+x*--x/2+1));}  n is 0-indexed, x is 1-indexed, outputs the numbers new-line delimited and reversed. Try it online. A pretty-printed result in correct order is 8 6 bytes longer: Try it online. Haskell, 67 bytes i#l=cycle((++)=<<(0<$)$[i..l-1]):l#(l+l-i+1) n!x=take x$(!!n)<$>1#2  Try it online! i#l -- function # builds the infinite matrix -- input: i and l are lowest and highest+1 non-zero number of -- the current line = cycle -- for the current line, repeat infinitely [i..l-1] -- the non-zero numbers of the line appended (++)=<<(0<$)    --   to a list of 0s with the same length
:                 -- append next row of the matrix
l#(l+l-i+1)     --   which is a recursive call with i and l adjusted

n!x =                -- main function
1#2    -- create matrix
(!!n)<\$>       -- pick nth element of each row
take x             -- take the first x numbers thereof
`