Progression of Matrix Columns

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 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.

AdmBorkBork

Posted 2018-05-16T14:28:24.993

Reputation: 41 581

Answers

4

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

Arnauld

Posted 2018-05-16T14:28:24.993

Reputation: 111 334

3

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.

Giuseppe

Posted 2018-05-16T14:28:24.993

Reputation: 21 077

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?

– JayCe – 2018-05-16T15:53:33.103

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 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. – Giuseppe – 2018-05-16T16:04:52.343

2

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!

Rod

Posted 2018-05-16T14:28:24.993

Reputation: 17 588

2

APL (Dyalog Classic), 27 24 23 bytes

-1 thanks to @FrownyFrog

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

Try it online!

ngn

Posted 2018-05-16T14:28:24.993

Reputation: 11 449

1 shorter {+\⍵/2}|⊣ – FrownyFrog – 2018-05-16T18:36:27.963

2

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

Giuseppe

Posted 2018-05-16T14:28:24.993

Reputation: 21 077

1

Jelly, 11 bytes

ṖS+R¬;$)⁹ịⱮ

Try it online!

-1 thanks to Jonathan Allan.

Argument 1: x
Argument 2: n + 1

Erik the Outgolfer

Posted 2018-05-16T14:28:24.993

Reputation: 38 134

0ṁ;Ɗ -> ¬;$ saves a byte. – Jonathan Allan – 2018-05-16T21:34:07.360

@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 ;¬$). – Erik the Outgolfer – 2018-05-17T11:24:16.333

1

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]

ბიმო

Posted 2018-05-16T14:28:24.993

Reputation: 15 345

1

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!

user48543

Posted 2018-05-16T14:28:24.993

Reputation:

1

K (ngn/k), 33 31 bytes

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

Try it online!

ngn

Posted 2018-05-16T14:28:24.993

Reputation: 11 449

1

Haskell, 75 bytes

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

Try it online!

ბიმო

Posted 2018-05-16T14:28:24.993

Reputation: 15 345

1

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.

Chas Brown

Posted 2018-05-16T14:28:24.993

Reputation: 8 959

1

Perl 5 -n, 52 bytes

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

Try it online!

Xcali

Posted 2018-05-16T14:28:24.993

Reputation: 7 671

1

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".

Magic Octopus Urn

Posted 2018-05-16T14:28:24.993

Reputation: 19 422

0

Charcoal, 19 bytes

IE…·¹N§⁺Eι⁰EιL⊞Oυωη

Try it online! Link is to verbose version of code. Explanation:

    ¹               Literal 1
     N              First input (`x`) as a number
  …·                Inclusive range
 E                  Map (value `i`, 0-indexed counter `k`)
         ι          Current value
        E           Map over implicit range
          ⁰         Literal 0 (i.e. create array of `i` zeros)
            ι       Current value
           E        Map over implicit range
                 ω  Arbitrary variable
                υ   Predefined array
              ⊞O    Push
             L      Length
       ⁺            Concatenate arrays
      §           η Index by second input (`n`)
I                   Cast to string and implicitly print on separate lines

The snippet EιL⊞Oυω 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.

Neil

Posted 2018-05-16T14:28:24.993

Reputation: 95 035

0

Java 8, 65 63 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.

Port of @Arnauld's JavaScript (ES6) answer.

Try it online.
A pretty-printed result in correct order is 8 6 bytes longer: Try it online.

Kevin Cruijssen

Posted 2018-05-16T14:28:24.993

Reputation: 67 575

0

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

nimi

Posted 2018-05-16T14:28:24.993

Reputation: 34 639