g | x | w | all
Bytes Lang Time Link
131Python240906T174427ZNikoNyrh
087Clojure240906T143614ZNikoNyrh
008Vyxal240812T231156Zemanresu
047Ruby240815T111420ZG B
6860Wolfram Language Mathematica240820T025226Z138 Aspe
050JavaScript240814T040231Znoodle p
048Python 2240812T234108ZLucenapo
024K ngn/k240814T142209Zatt
082R240813T202411Zint 21h
051R240814T114338ZDominic
052APL+WIN240813T170209ZGraham
01305AB1E240813T091741ZKevin Cr
033Charcoal240813T002546ZNeil
044JavaScript Node.js240812T195454Zemanresu
066JavaScript Node.js240812T195248Zl4m2

Python, 131 bytes

import numpy
lambda a,b,c,n:numpy.arange(b*c*n).reshape((-1,b,c)).transpose((0,2,1)).reshape((-1,a,b)).sum(axis=(1,2)).flatten()[n]

TIO. Unlike the other Python answer, this re-constructs the entire grid and uses Numpy's ndarray manipulation to squeeze out the correct numbers. You get the complete summed grid if you remove .sum(axis=(1,2)).flatten()[n]. It was very helpful when debuggin :)

Interesting how this is longer than my Clojure solution, while here I'm using specially-built Numpy operations and Clojure uses general-purose partition.

Clojure, 87 bytes

#(for[P[partition]p(P %2(P(quot %3 %1)(P %1(range))))r(apply map concat p)](apply + r))

TIO. Yay, Clojure is quite good with partitioning and "transposing" lists. This doesn't take any mathematical short-cuts, as you can see. apply map concat was a bit tricky to get right. This anonymous function evaluates into an infinite sequence of sums.

Vyxal, 8 bytes

Þ:ẇẇṠfẇṠ

Try it Online! Outputs an infinite list given w,y,x.

Þ:       # 0...infinity
  ẇ      # Cut into chunks of length w
   ẇṠ    # Sum groups of length y
     fẇṠ # Flatten, and sum groups of length x

Ruby, 46 47 bytes

->w,x,y,n{(n*x/w*w*y*2+n*x%w*2+y*w+~w+x)*x*y/2}

Try it online!

Short explanation: first get the first and last number of a chunk, then sum and multiply by x"y/2

Thanks @emanresu A for +1 byte. Actually, for fixing an oversimplification which led to a wrong result in some cases.

Wolfram Language (Mathematica), 68 60 bytes

Saved 8 bytes thanks to @noodle person

Try it online!

f[w_,x_,y_,n_]:=x*y(y*w⌊n*x/w⌋+Mod[n*x,w]+(y*w-w+x-1)/2)

JavaScript, 50 bytes

(n,x,y,w)=>x*y*(y*w*(x*n/w|0)+(w*~-y+x-1)/2+x*n%w)

Attempt This Online!

This uses a very similar method to emanresu A's solution, though I found it independently.

Explanation: We can view the problem as dealing with arithmetic series.

Here's the first chunk from the example in the question:

+----------+
|  0  1  2 |
| 12 13 14 |
| 24 25 26 |
| 36 37 38 |
+----------+

Take the vertical sums of each column:

0+12+24+36 = 72
1+13+25+37 = 76
2+14+26+38 = 80

This is an arithmetic sequence with first term 72 and common difference of 4. The 4 comes from the value of y, and the 72 comes from an arithmetic series of y terms with first term zero and common difference w.

Except, the first term of that inner arithmetic series isn't always zero. Look at the first chunk of the second column:

+----------+
| 48 49 50 |
| 60 61 62 |
| 72 73 74 |
| 84 85 86 |
+----------+

Here, the first term isn't 0 - it's 48. We get this 48 by multiplying y, w, and the column number of the cell. In JS, this is written as y*w*(x*n/w|0) (where |0 floors a number).

Now we can get the sum of any cell in the first column, but we still need to account for the difference between rows.

The sums of the first row are 228, 264, 300, and 336. The difference between each sum is 36, which is \$ x^2y \$. We multiply that by the row number, giving x*n%w*x*y. Here, x*n%w gives us the row number multiplied by x.

Okay, that's all we need to write this all out in JS. Remember that the formula for an arithmetic series is \$ \frac{n}{2}(2a+(n-1)d) \$, where n is the number of terms, a is the first term, and d is the common difference.

x/2*(2*y/2*(2*w*y*(x*n/w|0)+(y-1)*w)+(x-1)*y)+x*n%w*x*y

Now reduce this expression to its final golfed form:

x/2*(2*y/2*(2*w*y*(x*n/w|0)+(y-1)*w)+(x-1)*y)+x*n%w*x*y
x/2*(y*(2*w*y*(x*n/w|0)+(y-1)*w)+(x-1)*y)+x*n%w*x*y // 2*y/2 = y
x/2*(y*(2*w*y*(x*n/w|0)+~-y*w)+~-x*y)+x*n%w*x*y     // (y-1) = ~-y
x*(y*(2*w*y*(x*n/w|0)+~-y*w)+~-x*y)/2+x*n%w*x*y     // rearrange
x*y*(2*w*y*(x*n/w|0)+~-y*w+~-x)/2+x*n%w*x*y         // factor out y
x*y*((2*w*y*(x*n/w|0)+~-y*w+~-x)/2+x*n%w)           // factor out x*y
x*y*(w*y*(x*n/w|0)+(~-y*w+~-x)/2+x*n%w)             // factor out 2

Python 2, 58 54 53 51 49 48 bytes

lambda x,y,w,n:x*y*~(x*~(n*y*2-(n%w*2-w)*~-y))/2

Try it online!

Here w means the number of x-wide chunks in a row.

K (ngn/k), 24 bytes

{+/y/(!x)+x*(0,_y%x 1)\}

Try it online!

Input [y x;w]. Returns a function that computes the \$n\$th term of the sequence.

I think the dumb approach is shorter.

R, 85 82 bytes

-3 bytes thanks to pajonk

\(x,y,w,`[`=matrix,s=colSums)repeat show(s(s(((y*w*F):(y*w*(F=F+1)-1))[x])[y,,T]))

Just to see the source and the bytecount: Attempt This Online!

A user-friendly version: Test cases at TIO

A function that takes x,y and w as an input and outputs indefinetely the sequence of the sums.

Explanation (a naive approach):

Let's take \$X=3\$, \$Y=4\$, \$W=12\$ as an example. First, we obtain a matrix with the dimensions X × W*Y/X:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
[1,]    0    3    6    9   12   15   18   21   24    27    30    33    36    39    42    45
[2,]    1    4    7   10   13   16   19   22   25    28    31    34    37    40    43    46
[3,]    2    5    8   11   14   17   20   23   26    29    32    35    38    41    44    47

Calculate the column-sums:

   3  12  21  30  39  48  57  66  75  84  93 102 111 120 129 138

Rearrange them into the matrix Y × W/X:

     [,1] [,2] [,3] [,4]
[1,]    3   12   21   30
[2,]   39   48   57   66
[3,]   75   84   93  102
[4,]  111  120  129  138

Calculate the column-sums:

228 264 300 336

Proceed with the next part of the sequence Y*W to 2*Y*W-1 etc.

R, 52 51 bytes

Edit: -1 byte by taking g directly as a parameter

\(n,x,y,g)y*x*((y*g-g+1)*x/2+n%/%g*g*x*y+n%%g*x-.5)

Attempt This Online!

Started from:

xychunks=
function(n,l,w,h){
    startpos=n%%(l/w)*w+n%/%(l/w)*h*l
    increment=outer(1:w-1,(1:h-1)*l,`+`)
    sum(startpos+increment)
}

and gradually re-arranged from there.

The final golfed form seems quite similar to some answers in other languages, and I suspect that different initial approaches will converge to similar shortest re-arranged structures...


R + pryr, 44 bytes

f(y*x*((y*g-g+1)*x/2+n%/%g*g*x*y+n%%g*x-.5))

Try it at rdrr.io

Same as above, but the using the pryr package to 'guess' the function arguments from the code.

APL+WIN, 52 bytes

Prompts for a vector of n,x,y and w/x. Outputs first n terms. Index origin = 0

(n x y w)←⎕⋄n↑,+⌿[1] +/¨(n,y,w)⍴(+\0=x|i)⊂i←⍳n×x×y×w

Try it online! Thanks to Dyalog Classic

05AB1E, 14 13 bytes

∞<IôIô€øO˜IôO

-1 byte thanks to @emanresuA.

Inputs in the order \$W,Y,X\$. Outputs the infinite sequence.

Try it online.

Explanation:

∞              # Push an infinite positive list: [1,2,3,...]
 <             # Decrease each by 1 to a non-negative infinite list: [0,1,2,...]
  Iô           # Split it into groups of the first input W
    Iô         # Split those into groups of the second input Y
      €        # Map over each inner matrix:
       ø       #  Zip/transpose; swapping rows/columns
        O      # Sum each inner-most list
         ˜     # Flatten the infinite list of lists
          Iô   # Split it into groups of the third input X
            O  # Sum each inner list again
               # (after which the infinite list is lazily output implicitly)

Charcoal, 38 33 bytes

NθNηNζNεI÷××θη⊖×θ⊕⁻⊗×εη×⁻⊗﹪εζζ⊖η²

Try it online! Link is to verbose version of code. Takes X, Y, W/X, n as inputs. Explanation: Looks like I've found the same closed formula. Really, verbose mode says it all, except of course the variables have to be called q, h, z, and e instead, just in case you want to save 8 bytes by requiring input in JSON format. Edit: Saved 5 bytes by porting @xnor's golf to @Lucenaposition's answer.

JavaScript (Node.js), 48 47 46 43 42 48 46 45 44 bytes

(n,x,y,w)=>x*y*(n*x*y+(--y*w+x-1)/2-n*x%w*y)

Try it online! -3ish thanks to Arnauld, -1 thanks to xnor, hopefully now actually works. There might be a shorter version taking w/x instead.

Simple numeric formula. They say a picture speaks a thousand words:

enter image description here

JavaScript (Node.js), 66 bytes

(x,y,w,n)=>(g=i=>i&&(i/w/x/y^n/w|i/x%w^n%w?0:i)+g(i-1))(x*w*~y*~n)

Try it online!

Input W ÷ X

Silly.